Website Design United States, Website Design California, Website Designing United States, Website Designing California

PHP and text file Guestbook - PHP

A question at the Programmer's Corner forums led to the creation of a PHP guestbook that uses a text file to store the entered information from visitors. This is an alternative when you may not have a Database available or aren't familiar with XML.

The source code is a working example that can be copied and pasted into a file. At the very top of the code there are 3 variables that you may need to change. Details for those variables can be found in comments in the code.

The script does require you to create a text file that is blank. This means it initially cannot contain any - no spaces, line feeds, characters. Otherwise your data may not display correctly.

The input fields for the guestbook are:
Name
Screen Name *
Email *
Webpage
Sex
Comments *

- Items followed with a * are required fields.

This code allow you to add to the guestbook and has a link to only view the guestbook entries.

The layout/design is handled with XHTML and CSS so it can be changed easily.


<?php
/* *************************************************************************
Below are the variables that you will want to edit for this script.
************************************************************************* */
// Your name - or your website's name. This will make the script a
// little more personal.
$YOUR_NAME = "Your Name";

// $guestbookfile is the name of the text file that will store all your
// guestbook entries. When you first create this file it MUST be blank.
// That means nothing in the file, not even a blank line.
$guestbookfile = "guestbook.txt";

// $_SERVER[PHP_SELF] should recognize this file so it can be recalled.
// If that does not work then set $PHP_SELF equal to the name of the
// php file you put this script into.
// example: $PHP_SELF = "guestbook.php";
$PHP_SELF = $_SERVER[PHP_SELF];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>PHP and Text File Guestbook</title>

<style type="text/css" media="all">
body{ margin: 10px; padding: 0; background-color: #fff; color: #333; font-size: 62.5%; text-align: center; }
*{ font-family: Verdana, Arial, Sans-Serif; }
.bold{ font-weight: bold; }
input, textarea{ font-size: 1.1em; }
textarea{ height: 50px; }
form{ margin: 0 0 10px 0; padding: 0; font-size: 1em; }
td.right{ text-align: right; }
td.center{ text-align: center; }
td.top{ vertical-align: top; }
td.divider{ margin: 5px 0; padding: 0; border-top: 1px solid #009; }
td.last{ margin: 5px 0; padding: 0; }
#gb{ margin: 0 auto; padding: 0 0 10px 0; background-color: #def; color: #333; border: 1px solid #009; width: 300px; }
#gb table{ margin: 0 auto; text-align: left; }
#gb h1{ margin: 2px 3px 5px 3px; padding: 3px 0 4px 0; background-color: transparent; color: #333; font-size: 1.2em; text-align: center; border-bottom: 1px dashed #009; }
p{ margin: 0; padding: 5px; background-color: transparent; color: #333; font-size: 1em; text-align: left; font-style: italic; }
</style>

</head>
<body>

<?php
// Email function check
// returns true if email is in valid form, otherwise false
function check_email($email){
if(ereg( "^[^@ ]+@([a-zA-Z0-9.\-]{1,63})(\.)+([a-zA-Z0-9\-]{2}|aero|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro)\$",$email)){
return TRUE;
}else{
return FALSE;
}
}

if($_GET['showdata'] == "yes"){
$handle = fopen("$guestbookfile", "r");

// Count is used when getting the lines of the file.
$count = 0;

echo "<div id=\"gb\"><h1>$YOUR_NAME's Guestbook</h1><table>";

while(!feof($handle)){
// Get each line from the file
$buffer = fgets($handle, 4096);

// Get the information when $count <= 5, otherwise display the
// information and reset $count = 0.
switch($count){
case 0: $name = str_replace("\r\n", "", $buffer); $count++; break;
case 1: $screenname = str_replace("\r\n", "", $buffer); $count++; break;
case 2: $email = str_replace("\r\n", "", $buffer); $count++; break;
case 3: $uri = str_replace("\r\n", "", $buffer); $count++; break;
case 4: $sex = str_replace("\r\n", "", $buffer); $count++; break;
case 5: $comments = str_replace("\r\n", "", $buffer); $count++; break;
default:
// Since we've gathered all the data for one entry, we can now show the output.
echo "
<tr><td class=\"right\">Name:</td><td>"; if($name != ""){ echo $name; }else{ echo ""; }echo "</td></tr>
<tr><td class=\"right\">Screen Name:</td><td>$screenname</td></tr>
<tr><td class=\"right\">Email:</td><td><a href=\"mailto:$email\" title=\"Send $screenname an email\">$email</a></td></tr>
<tr><td class=\"right\">Webpage:</td><td>"; if($uri != ""){ echo "<a href=\"$uri\" title=\"$screenname - webpage\">$uri</a>"; }else{ echo ""; }echo "</td></tr>
<tr><td class=\"right\">Sex:</td><td>$sex</td></tr>
<tr><td class=\"right\">Comments:</td><td>$comments</td></tr>
<tr><td colspan=\"2\" class=\"divider\">&nbsp;</td></tr>";
$count = 0;
break;
}
}
echo "
<tr><td colspan=\"2\" class=\"last\"><a href=\"$PHP_SELF\" title=\"Back to guestbook entry form\">Back to guestbook entry form</a></td></tr>
</table></div>";

fclose($handle);
}else{
/*
Since COMMENTS and SCREENNAME are required we need to check them.
Also to check to make sure that the EMAIL is a valid email,
we need to gather that as well.

The stripslashes() is used to remove any backslash added by
PHP when the form is submitted. This may not be needed
depending on the settings in your PHP.INI file.

trim() removes the whitespaces from before and after the
variable.
*/

if(isset($_POST['comments'])){ $comments = stripslashes(trim($_POST['comments'])); }else{ $comments = ""; }
if(isset($_POST['screenname'])){ $screenname = stripslashes(trim($_POST['screenname'])); }else{ $screenname = ""; }
if(isset($_POST['email'])){ $email = stripslashes(trim($_POST['email'])); }else{ $email = ""; }

/*
We use str_replace() to replace to check COMMENTS and SCREENNAME.
This is incase the user puts a bunch of spaces in the
name but no characters.

We check to make sure that EMAIL is valid.

We show the form if:
- COMMENTS is blank
- SCREENNAME is blank
- EMAIL is in an invalid form
*/

if(str_replace(" ", "", $comments) == "" OR str_replace(" ", "", $screenname) == "" OR !check_email($email)){

// Once again gather the data but don't trim the data.
$comments = stripslashes($_POST['comments']);
$name = stripslashes($_POST['name']);
$sex = stripslashes($_POST['sex']);
$email = stripslashes($_POST['email']);
$screenname = stripslashes($_POST['screenname']);
$uri = stripslashes($_POST['uri']);

$checked = " checked=\"checked\" ";

// Show the form and load it will the values that the user may have already filled in.
echo "
<div id=\"gb\">
<h1>PHP Guestbook</h1>
<p>* Items in <span class=\"bold\">bold</span> are required</p>
<form action=\"$PHP_SELF\" method=\"post\">
<table>
<tr>
<td class=\"right\">Name:</td>
<td><input type=\"text\" name=\"name\" value=\"$name\" /></td>
</tr>
<tr>
<td class=\"right\"><span class=\"bold\">Screen Name:</span></td>
<td><input type=\"text\" name=\"screenname\" value=\"$screenname\" /></td>
</tr>
<tr>
<td class=\"right\"><span class=\"bold\">Email:</span></td>
<td><input type=\"text\" name=\"email\" value=\"$email\" /></td>
</tr>
<tr>
<td class=\"right\">Webpage:</td>
<td><input type=\"text\" name=\"uri\" value=\"$uri\" /></td>
</tr>
<tr>
<td class=\"right top\">Sex:</td>
<td>
<input type=\"radio\" id=\"male\" name=\"sex\" value=\"male\""; if($sex == "male") echo $checked; echo "/> <label for=\"male\">Male</label> <br />
<input type=\"radio\" id=\"female\" name=\"sex\" value=\"female\""; if($sex == "female") echo $checked; echo "/> <label for=\"female\">Female</label> <br />
<input type=\"radio\" id=\"none\" name=\"sex\" value=\"none\""; if($sex != "male" AND $sex != "female") echo $checked; echo "/> <label for=\"none\">I don't wish to say</label>
</td>
</tr>
<tr>
<td class=\"right top\"><span class=\"bold\">Comments:</span></td>
<td><textarea name=\"comments\">$comments</textarea></td>
</tr>
<tr>
<td colspan=\"2\" class=\"center\">
<input type=\"reset\" />
<input type=\"submit\" />
</td>
</tr>
</table>
</form>
<a href=\"$PHP_SELF?showdata=yes\" title=\"Show guestbook entries\">Show guestbook entries</a>
</div>
";

}else{
/*
If we're in this section it means that the user has input the COMMENTS and
SCREENNAME fields and the EMAIL field was valid.
*/

// Get the info
$comments = stripslashes(trim($_POST['comments']));
$name = stripslashes(trim($_POST['name']));
$sex = stripslashes(trim($_POST['sex']));
$email = stripslashes(trim($_POST['email']));
$screenname = stripslashes(trim($_POST['screenname']));
$uri = stripslashes(trim($_POST['uri']));

// If not sex was selected, or they person did not want to disclose their
// sex - we fill the variable with 'n/a'.
if($sex == "none"){ $sex = "n/a"; }

// replace all the new lines (Windows) for the text vairable.
$comments = str_replace("\r\n", " ", $comments);

// Open $guestbookfile for appending
$file = fopen($guestbookfile, "a");

// Write to the file.
// Include the \r\n to emulate hitting enter on a Windows machine.
fwrite($file, "$name\r\n$screenname\r\n$email\r\n$uri\r\n$sex\r\n$comments\r\n\r\n");
// Close the file.
fclose($file);

$handle = fopen("$guestbookfile", "r");

// Count is used when getting the lines of the file.
$count = 0;

echo "<div id=\"gb\"><h1>$YOUR_NAME's Guestbook</h1><table>";

while(!feof($handle)){
// Get each line from the file
$buffer = fgets($handle, 4096);

// Get the information when $count <= 5, otherwise display the
// information and reset $count = 0.
switch($count){
case 0: $name = str_replace("\r\n", "", $buffer); $count++; break;
case 1: $screenname = str_replace("\r\n", "", $buffer); $count++; break;
case 2: $email = str_replace("\r\n", "", $buffer); $count++; break;
case 3: $uri = str_replace("\r\n", "", $buffer); $count++; break;
case 4: $sex = str_replace("\r\n", "", $buffer); $count++; break;
case 5: $comments = str_replace("\r\n", "", $buffer); $count++; break;
default:
echo "
<tr><td class=\"right\">Name:</td><td>"; if($name != ""){ echo $name; }else{ echo ""; }echo "</td></tr>
<tr><td class=\"right\">Screen Name:</td><td>$screenname</td></tr>
<tr><td class=\"right\">Email:</td><td><a href=\"mailto:$email\" title=\"Send $screenname an email\">$email</a></td></tr>
<tr><td class=\"right\">Webpage:</td><td>"; if($uri != ""){ echo "<a href=\"$uri\" title=\"$screenname - webpage\">$uri</a>"; }else{ echo ""; }echo "</td></tr>
<tr><td class=\"right\">Sex:</td><td>$sex</td></tr>
<tr><td class=\"right\">Comments:</td><td>$comments</td></tr>
<tr><td colspan=\"2\" class=\"divider\">&nbsp;</td></tr>";
$count = 0;
break;
}
}
echo "
<tr><td colspan=\"2\" class=\"last\"><a href=\"$PHP_SELF\" title=\"Back to guestbook entry form\">Back to guestbook entry form</a></td></tr>
</table></div>";

fclose($handle);
}
}
?>
</body>
</html>


WEB DESIGN INDIA
42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Email: support.webdesignindia@gmail.com
Tel: 91-011-40502005, 9810067295

 















 


© 2008-2009 dotnet4all.com