PHP: Lesson 6 Simple Guestbook using PHP

A Tutorial

Authors

Written 2010 by Will Johnson for Fast Forward Technologies
Email Fast Forward Technologies at fft2001@aol.com
or post your comments for public view far below.
Creative Commons Attribution 3.0 License
fft2001
The Knol Self-Tweeter on the left, is designed to work only for an article's author.

Follow Fast Forward Technologies on Twitter!
or use my Knol Public activity feed

Buy your own Knol Self-Tweeter for only two dollars! I will email it to you, when I receive payment.
What the Knol Self-Twitter does exactly is explained at this link.

<-- Start at PHP: Lesson 1 "Hello World"
<-- Back to PHP: Lesson 5 Send Email using a PHP Script

As you've noticed by now, my tutorials are "get your hands right in it and get dirty".  I don't monkey around with showing you 49 options to every stupid command.  Now we're going to look at a way to make a very simple guestbook or bulletin board or posting log or whatever you want to call it.  It just asks the user for a name and their comments and then slaps it up at the top of a log of the most recent comments with a time and date stamp.

Obviously it's subject to spam and obnoxious comments and so on.  We're just looking at it, to learn how to do this sort of thing.  Not because it's the best solution.

First the HTML form, call it something like MyCommentForm.html or whatever

<html><head><title>Post Your Comments!</title></head>

<body>
<form method = "post" action = "PostItem.php">
    Name: <input type = "text" name = "name"><br>
    Comments:<br><textarea cols=22 rows=6
        name="comments" wrap="virtual"></textarea><br>
    <input type = "submit"> <input type = "reset">
</form>
</body>
</html>

The textarea is a box, where the user can enter free-form comments.  When you use it, go ahead and adjust the number of columns and rows to whatever you think your readers might like.  The "virtual" wrap means the user sees the text wrapping in the box, but the text passed to the PHP script won't include the wrapping.  The submit button is obvious, the reset allows the reader to blank their response and start again.

Now the PHP script which you save in file PostItem.php
That's the name we gave it above in the action directive.  Of course it you change it above, you'd also change where you save this here to match.


<?php
// Written by Will Johnson, Fast Forward Technologies 2010
// You must set your output file to permission 666
//   or this won't work!

    $file=fopen("mylogfile.txt","r") or exit("Unable to Open File");
    $contents = fread($file, 8192);
    fclose($file);
    $datetime = date('l dS \of F Y h:i:s A');

    $file = fopen("mylogfile.txt","w");
    $new = "On ".$datetime."<br>".$_POST["name"]." writes <br>";
    $new = $new.$_POST["comments"]."<br><br>";
    fputs($file,$new.$contents);
    fclose($file);
?>
<html><head><title>Recent Comments</title></head>
<body>
Simple Posting Log - Created 2010 by Will Johnson <a href = "mailto:wjhonson@aol.com">wjhonson@aol.com</a> of<br>
<a href = "http://tinyurl.com/DatabaseDesigns">Web and Database Designs of Santa Cruz</a><br><br>
<?php echo $new.$contents; ?>
</body>
</html>


As those of you paying attention can see, the file contents we're limiting to only the top 8192 characters.  You can make this smaller if you just want to see one "page" of comments.  The older comments just fall off into limbo, as the newer ones are posted to the top.  Don't worry right now, about what the date formatting codes mean.  They were designed by a group of monkeys.

What we're doing here in short English, is we open the log file and read it into the contents variable then close it.  Then we reopen it again for writing, post the new comment to the top, then add the contents variable, which is all of the old comments, to the bottom, then close it again.

Then we output the HTML code and copy the contents to the screen (that's what the "echo" is doing) so the user can see their comment at the top of all the other comments.  As you can tell this design doesn't let you, the admin, delete any comments, except just to wipe the file out entirely.  Of course you can always edit it in Notepad and remove a comment if you wanted.


<-- Start at PHP: Lesson 1 "Hello World"
<-- Back to PHP: Lesson 5 Send Email using a PHP Script

Comments

cheap shoes

A great post but How to select a cool shoes. we sell <a href="http://www.cheap-shoes-online.org" title="cheap shoes">cheap shoes</a> We are the best store provided various <a href="http://www.cheap-shoes-online.org" title="cheap designer shoes">cheap designer shoes</a> but only a little white to make <a href="http://www.cheap-shoes-online.org" title="coach shoes online">coach shoes online</a>

Anonymous - 28 May 2011

cheap handbags

A great post but How to select a cool handbags. we sell <a href="http://www.designerhandbags-outlet.com" title="cheap handbags">cheap handbags</a> We are the best store provided various <a href="http://www.designerhandbags-outlet.com" title="cheap designer handbags">cheap designer handbags</a> but only a little white to make <a href="http://www.designerhandbags-outlet.com" title="coach handbags online">coach handbags online</a>

Anonymous - 05 May 2011