|
|
|
|
|
|
|
|
|
|
|
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:
Figure 1: Shows the steps involved with the execution of embedded PHP code.
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'; ?>
<br> Bill Rosener <br> Northeastern State University <br> Management Information Systems <br> (918) 456-5511 Ext. 2923 <br>
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>