[Tutorial] Guestbook

Example of PHP code.

Moderator: Moderators

[Tutorial] Guestbook

Postby DooBDee on Sat May 14, 2005 4:57 pm

Code: Select all
##############################################################
## Script Title: GuestBooK
## Script Author: DooBDee - http://www.doobdee.net
## Script Description: This script is a guestbook, where people
##                            will leave their comments and will be saved
## Script Version: 1.0.0
##
## Script Level: (Easy)
## Script Implementation: 5 Minutes
## Script Files: [guestbook.php, action.php, comments.php, log.txt] - Plain Files
##############################################################
## If you have troubles with this script and are unsure how to
## implement it, please contact: http://www.doobdee.net, from
## where you will receive support for it.
##############################################################


Greetings, a simple guestbook :

1) Firstly create a simple text file.
In this thread, we will call it log.txt

2) Now create a very basic HTML form. Like so :

Code: Select all
<html>
<head>
<title>Guestbook</title>
</head>
<body>
<form action="action.php" method="post">
<br>
<p>Please enter your name <br></p>
<input type="text" name="name" />
<br>
<p>Your email address*<br></p>
<input type="text" name="email" />
<br>
<p>Please enter your Subject <br></p>
<input type="text" name="subject" />
<br>
<p>Your Comments<br></p>
<textarea rows="7" name="comments" cols="40"></textarea>
<br>
<input type="submit" />
</form>
</body>
</html>


3) We will now need to save the comment, and use action.php to do this.

Code: Select all
<?php
if(!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['subject']) || !isset($_POST['comments']))
{
  die('You did not specify any fields');
}

// Fetches $_POST and makes it equal to appropriate variable and checks to see if it exists
$name = isset($_POST['name']) ? stripslashes($_POST['name']) : '';
$email = isset($_POST['email']) ? stripslashes($_POST['email']) : '';
$subject = isset($_POST['subject']) ? stripslashes($_POST['subject']) : '';
$comments = isset($_POST['comments']) ? stripslashes($_POST['comments']) : '';

// Define Date
$today = date("Y-m-d");

// Writes to file
$fh = fopen("log.txt" , "a");
fwrite($fh,$today);
fwrite($fh,"\n");
fwrite($fh,"<b>Name:</b> ");
fwrite($fh,$name);
fwrite($fh,"\n");
fwrite($fh,"<b>Email:</b> ");
fwrite($fh,$email);
fwrite($fh,"\n");
fwrite($fh,"<b>Subject:</b> ");
fwrite($fh,$subject);
fwrite($fh,"\n");
fwrite($fh,"<b>Comments:</b> ");
fwrite($fh,$comments);
fwrite($fh,"\n");
fwrite($fh,"\n");
fclose($fh);
?>

<html>
<head>
<title>Message Sent!</title>
</head>
<body>

<?php
echo "Message Sent";
echo "<br>";
echo "<br>";
echo "Click <a href=\"comments.php\">Here</a> to see comments";
?>

</body>
</html>
?>


4) We will now need to create a file that can read from the log file. We do this like so :

Code: Select all
<html>
<head>
<title>View Comments</title>
</head>
<body>
<p><u>Guests Comments</u>
<pre>
<?php 
if(!@file_exists('log.txt')) 

die('Cannot open file.'); 

$str = file_get_contents('log.txt'); 
echo $str; 
?>
</pre>
</body>
</head>


For more downloads and information, visit : http://www.doobdee.net/site/viewpage.php?p=4
DooBDee
Website Ninja
DooBDee.net
User avatar
DooBDee
Moderator
Moderator
 
Posts: 3530
Joined: Tue Mar 01, 2005 6:28 pm
Location: DooBDee.net

Postby DooBDee on Sat Jul 09, 2005 5:30 pm

Greetings,

Sript updated:

Code: Select all
<?php
/*   Title: guestbook.php
*   Version: 2.0.0
*   Author: DooBDee
*   Website: http://www.doobdee.net
*   Description: A multiple guestbook page, that runs on a flat file.
*/

//   Mode for retrieving comments
switch( $_GET['mode'] )
{
   case 'comments' :
   $contents = file_get_contents( "log.txt" );
   echo $contents;
   die;
   break;
}

$name = isset($_POST['name']) ? stripslashes($_POST['name']) : ''; 
$email = isset($_POST['email']) ? stripslashes($_POST['email']) : ''; 
$subject = isset($_POST['subject']) ? stripslashes($_POST['subject']) : ''; 
$comments = isset($_POST['comments']) ? stripslashes($_POST['comments']) : ''; 

$required = array("name"  => "Your Name",
                  "email" => "Email Address",
                  "subject" => "Your Subject",
                  "comments" => "Your Comments",

);

foreach($required as $field => $label)
{
   if (!$_POST[$field])
{
   $warnings[$field] = "Required";
   }
}

if ($_POST["email"] &&
   !ereg("^[^@]+@([a-z\-]+\.)+[a-z]{2,4}$", $_POST["email"]))
   $warnings["email"] = "Invalid email";

if (count($warnings) > 0) {

//   Start output...
?>
<html>
<head>
<title>Guestbook</title>
</head>
<body>
<form action="guestbook.php" method="post">
<br>
<p><b>Your Name</b><br></p>
<input type="text" name="name" VALUE="<?php echo $_POST["name"];?>"><?php echo $warnings["name"];?>
<br>
<p><b>Email Address</b><br></p>
<input type="text" name="email" VALUE="<?php echo $_POST["email"];?>"><?php echo $warnings["email"];?>
<br>
<p><b>Your Subject</b><br></p>
<input type="text" name="subject" VALUE="<?php echo $_POST["subject"];?>"><?php echo $warnings["subject"];?>
<br>
<p><b>Your Comments</b><br></p>
<textarea rows="7" name="comments" cols="40"><?php echo $_POST["comments"];?></textarea><?php echo $warnings["comments"];?>
<br>
<p><input type="submit" value="Sign Guestbook"></p>
</form>
</body>
</html>

<?php
}
else
{
   // Define Date
   $today = date("Y-m-d");

   // Writes to file
   $fh = fopen("log.txt" , "a");
   fwrite($fh,$today);
   fwrite($fh,"<br>");
   fwrite($fh,"<b>Name:</b> ");
   fwrite($fh,$name);
   fwrite($fh,"<br>");
   fwrite($fh,"<b>Email:</b> ");
   fwrite($fh,$email);
   fwrite($fh,"<br>");
   fwrite($fh,"<b>Subject:</b> ");
   fwrite($fh,$subject);
   fwrite($fh,"<br>");
   fwrite($fh,"<b>Comments:</b> ");
   fwrite($fh,$comments);
   fwrite($fh,"<br>");
   fwrite($fh,"<br>");
   fclose($fh);
   echo "Message Entered!  Click <a href=\"guestbook.php?mode=comments\">Here</a> to see comments";
}
?>


Hope this is helpfull.
Last edited by DooBDee on Thu Feb 16, 2006 5:07 pm, edited 1 time in total.
DooBDee
Website Ninja
DooBDee.net
User avatar
DooBDee
Moderator
Moderator
 
Posts: 3530
Joined: Tue Mar 01, 2005 6:28 pm
Location: DooBDee.net

Postby Phantom on Sat Jul 09, 2005 5:42 pm

In lines 80 through 94, you can change it to one variable. Insert this on line 77:

Code: Select all
$message = <<<HTML
$today<br />
<b>Name:</b> $name<br />
<b>Email:</b> $email<br />
<b>Subject:</b> $subject<br />
<b>Comments:</b> $comments<br /><br />
HTML;

Now replace lines 80 through 94 with this:

Code: Select all
fwrite($fh, $message);

That will make your coding a lot more cleaner than 14 lines with a word each.
"Best of the best of the best, sir! With honors!" -- MIB
[ jphantom.com ]
Phantom
Registered User
Registered User
 
Posts: 245
Joined: Mon Apr 18, 2005 3:24 am


Return to PHP Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest