TechVideos
  Home     About           Free Videos           FAQ     Contact  
Stay Informed Tell A Friend Bookmark this site
Copyright 2004

DEVELOPING ADVANCED WEB APPLICATIONS USING PHP

ABSTRACT

For a web site to be competitive today, it must be interactive, dynamic, and capable of performing on-line transactions. Gone are the days, when entire web sites were created using static pages. Today, web developers are using tools and programming languages to help them create customized web pages, develop shopping cart applications, and track user's behaviors. To meet these development challenges, this paper will examine the strengths and weakness of one of the latest programming languages used to create dynamic web pages known as "PHP Hypertext Preprocessor" (PHP). This software, originally known as Personal Home Pages, was developed in 1997 and is currently being used in over 10 million domains.

INTRODUCTION

Before PHP, developers used CGI (Common Gateway Interface) scripts for interacting with users, querying databases, etc. However, since CGI applications are separate, stand alone programs, scalability is missing because multiple CGI applications running concurrently can quickly consume all available memory. To avoid this problem, PHP was developed using a three tier architecture. The first tier is the client also known as the browser. The second tier is the server (including both the web server and PHP interpreter). The third tier is the database system. Using this approach, it avoids the forking of external programs found in CGI applications which can quickly consume all available memory.

With PHP you write HTML script with embedded code delineated with special start and end tags. The embedded code is executed on the server and then sent to your browser. What is displayed in the browser is the result from the code's execution. This is very different from other scripting languages like Perl and C where code is written to output HTML commands. While similar to JavaScript, the main difference with PHP is the code is executed on the servers, whereas with JavaScript the code is executed on the client-side. With JavaScript it is possible to determine the underlying code, whereas with PHP the underlying code is hidden from the user.

Some of the main advantages of PHP include:

Some of the disadvantages of PHP include:

The diagram below shows how the browser, web server, and database interact via the Internet. In step 1 the browser makes a request to the server. The embedded PHP code within the HTML document is executed in step 2. If a database is queried then steps 3 and 4 are taken. The results are then returned to the PHP program. In step 5 the results are returned to the server. Finally in step 6 the information is displayed in the browser.

Figure 1: Shows the steps involved with the execution of embedded PHP code.

EXAMPLES

Below are four PHP examples. Screen captures and source code are included for each example. The file names for the source code are underlined. (Note any file containing PHP code must end with the file extension "php").

Example 1 - Include a file.
Frequently the same headers, footers, or navigation buttons are used in a web site. These common elements help to give the site a consistent look and feel and make navigating through the site easier. Maintaining this common information across multiple documents can be a time-consuming process. To help simplify this process, PHP allows files to be included within one another. Now common information can be factored out into separate files and included as needed. Using this approach, information only needs to be updated in one file, rather than the time-consuming task of modifying numerous documents. Shown below are the screen capture and the source code to include a file.

Figure 2: Shows a screen capture of an included file.

example1.php

<title>Example 1 </title>
<h2> Example 1 </h2>
<?php
include 'footer.html';
?>


footer.html
<br>
Bill Rosener <br>
Northeastern State University <br>
Management Information Systems <br>
(918) 456-5511 Ext. 2923 <br>


Example 2 - Guest book.
The following example shows how to create an interactive guest book. Shown below are the screen capture and the source code to create a guest book.

Figure 3: Shows a screen capture of an interactive guest book.

example2.php

<html>
<title>Example 2</title>
<h2> Example 2 </h3>
Please sign our guest book.
<form method=post action=add-entry.php>
Name: <input name=username type=text size=20> <p>
Address: <input name=useraddress type=text size=20> <p>
Comments: <textarea name="comments"
WRAP=physical Rows=5 cols=40></textarea>
<p>
<input type=submit>
</form>
</html>

add-entry.php

<?php
$username=$_POST['username'];
$useraddress=$_POST['useraddress'];
$comments=$_POST['comments'];
$filename = 'guestbook.txt';

if (is_writable($filename)) {
  if (!$handle = fopen($filename, 'a')) {
    echo "Cannot open file ($filename)";
    exit;
  }
  $temp = $username."\r\n".$useraddress."\r\n".$comments."\r\n\r\n";
  if (fwrite($handle, $temp) == FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
  }
  echo "<h2>Thank you for signing my questbook.</h2>";
}
else {
  echo "The file $filename is not writable";
}
?>

Example 3 - Password protection.
The following example illustrates how to make grades available over the Internet using a username and password. The originally data was stored in MS Excel format. However, before posting grades the "Save As" option was selected choosing the file type "Text (MS-DOS)". Shown below are a screen capture and the source code to password protect a text file that contains tab delimited information.

Figure 4: Shows a screen capture of the password protected area.

example3.php

<html>
<title> Example 3 </title>
<h2> Example 3 </h3>
<form method=post action=login_check.php>
Enter your username: <input name=username type=text size=20> <p>
Enter your password: <input name=password type=password size=20> <br>
<p>
<input type=submit>
</form>
</html>


login_check.php

<?php
$username=$_POST['username'];
$password=$_POST['password'];
$filename = 'grades.txt';

if (!$fp = fopen($filename, 'r')) {
   echo "Cannot open file ($filename)";
   exit;
}

while (!feof($fp)) {
  $data = fgets($fp);
  $n = sscanf($data, "%s%s%s", &$name,&$code, &$grade);
  if (strtoupper($username) == strtoupper($name) AND $password == $code) {
    echo "Your grade is: " . $grade;
    fclose($fp);
    exit;
  }
}
echo "Incorrect username and password.";
?>

Example 4 - Customize a web page.
The example below shows how to customize a web page so that different text and graphics appear depending on the value of the campus entered.

Figure 5: Shows a screen capture of the customized graphics and text.

example4.php

<html>
<head>
<title>Example 4</title>
</head>
<body>
<h1> Example 4 </h1>
<form action="<?php echo$_SERVER['PHP_SELF']; ?>" method="post">
Enter your campus: <input type="campus" name="campus">
<input type="submit">
<p>

<?php
if (!empty($_POST['campus'])) {
  echo "Welcome to {$_POST['campus']}";
  echo "<br>";

  if ($_POST['campus'] == 'Tahlequah') {
    include 'tahlequah.html';
  }
  elseif ($_POST['campus'] == 'Broken Arrow') {
    include 'broken-arrow.html';
  }
  elseif ($_POST['campus'] == 'Muskogee') {
    include 'muskogee.html';
  }
}
else {
  echo "Welcome to NSU";
  echo "<br>";
  include 'nsu.html';
}
?>

</body>
</html>


Example 5 - Recording the remote host and referring page.

Example 6 - Displaying a different picture in the morning, afternoon, and night.

Example 7 - Displaying different pictures depending on the exact second a page is downloaded.




ACKNOWLEDGMENTS

I would like to thank John Rose from the University of Tennessee for his helpful collaboration.




Powered by Sphider