Ultimate Guestbook Tutorial: How to build a Guestbook with a honeypot, error checking, IP banning, pagination, e-mail notification and smilies with PHP and mySQL

Ultimate Guestbook Tutorial: How to build a Guestbook with a honeypot, error checking, IP banning, pagination, e-mail notification and smilies with PHP and mySQL

Only 3 things left to do. Show our guestbook entries, show smilies and install our pagination. How about we finish off this tutorial in that order then!

Make a new file in the templates folder called entries.php. Then open up skin.php and:

Under:

<tr>
		<td valign="top"><?php include('form.php');?></td>
	</tr>

Add:

<tr>
		<td height="10"></td>
	</tr>
	<tr>
		<td valign="top"><?php include('entries.php'); ?></td>
	</tr>

All this does is link the entries.php file into the skin.php file. Now lets open up our entries.php file:

<?php
	$query = mysql_query("SELECT *, UNIX_TIMESTAMP(`date`) as date FROM `entries` ORDER BY `date` DESC");
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<?php
		while($row = mysql_fetch_array($query)){
		$i++;
		if(($i % 2) == 0){
			$class = 'class="entry1"';
			$classbg = 'class="entriesbg1"';
		} else {
			$class = 'class="entry2"';
			$classbg = 'class="entriesbg2"';
		}
	?>
	<tr>
		<td valign="top" <?php echo $classbg; ?>>
		<table width="100%" cellpadding="0" cellspacing="0" border="0">
			<tr>
				<td valign="top" align="right">#<?php echo $row['id']; ?> Posted on: <?php echo date("d/m/y g:i a", $row['date']); ?></td>
			</tr>
			<tr>
				<td valign="top">
				<table width="100%" cellpadding="0" cellspacing="0" border="0" <?php echo $class; ?>>
					<tr>
						<td valign="top"><span>Name:</span></td>
						<td valign="bottom" width="100%"><?php echo stripslashes($row['name']); ?></td>
					</tr>
					<tr>
						<td valign="top" nowrap="nowrap"><span>E-Mail:</span></td>
						<td valign="bottom" width="100%"><?php echo stripslashes($row['email']); ?></td>
					</tr>
					<?php
						if(strlen($row['website']) > 0){
							if(substr(strtolower($row['website']), 0, 7) != 'http://'){
								$website = 'http://'.$row['website'];
							} else {
								$website = $row['website'];
							}
					?>
					<tr>
						<td valign="top" nowrap="nowrap"><span>Website:</span></td>
						<td valign="bottom" width="100%"><a href="<?php echo stripslashes($website); ?>" target="_blank"><?php echo substr($website,0,30); ?></a></td>
					</tr>
					<?php
						}
					?>
					<tr>
						<td valign="top" colspan="2"><span>Message:</span></td>
					</tr>
					<tr>
						<td valign="top" colspan="2"><?php echo stripslashes($row['message']); ?></td>
					</tr>
				</table>
				</td>
			</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td valign="top" height="10"></td>
	</tr>
	<?php } ?>
</table>

There is alot of CSS going on here, so here is the new CSS i added, add it to your stylesheet, or add your own:


.entries {
	padding: 10px 10px 0px 10px;
	border: #999999 solid 1px;
	font-size: 14px;
	background: #333333;
}

.entriesbg2 {
	padding: 10px;
	border: #999999 solid 1px;
	font-size: 14px;
	background: #111111;
}

.entriesbg1 {
	padding: 10px;
	border: #999999 solid 1px;
	font-size: 14px;
	background: #555555;
}

.entrytitle {
	font-family: Helvetica, Arial, Verdana, Courier;
}

.entrysmall {
	font-size: 12px;
}

.entry1 {
	padding: 5px;
	margin: 2px;
	border: #222222 solid 1px;
	background: url('../images/entry2.jpg') repeat-x #000000;
}

.entry2 {
	padding: 5px;
	margin: 2px;
	border: #555555 solid 1px;
	background: url('../images/entry1.jpg') repeat-x #333333;
}

a {
	color: #FFFFFF;
	text-decoration: none;
}

a:hover {
	text-decoration: underline;
}

Correct, there are some images i used. You can get them from the source pack, or make your own background images. Just make a folder in the templates folder called images, and drop the entry1.jpg and entry2.jpg files in there.

Now time for some explanation, as usual i will be using the line number to explain what is going on; but for the most part it does not require any explanation.

2: This is our SQL statement, which gets all of the entries, orders them by the date (newest first) and converts the mysql timestamp to a unix_timestamp, something php can understand.

6: This sets up a while statement, which runs through each logical row in the array, or the output that the SQL query generated.

7: Sets up a variable called $i which is self incrementing, meaning each time the while loop iterates (runs through) it increases its value by 1. Which is helpful for lines 8-14

8-14: This section helps us get alternating row colours. Well not really colours, in our case its styles. It uses the modulus operator, %. Which basically shows the remainder of a division. For example: What is the remainder or 3 going into 15. It would be 0, because 3×5 = 15, so it fits perfectly. But 3 % 5 would return 2. Because 3 only goes in once, leaving a remainder of 2. You get it?

Then the relevant classes are stored into variables, which will be overwritten each iteration.

17: Shows how we use this alternating rows. It simply outputs the variable and in turn chooses the right style.

20: This is the first line that shows us how to access our new array. We stored the SQL array into the variable $row. So now it can be accessed via: $row['id']; Each time the loop runs through the array steps through, so its like reading the table row by row, the variables don’t change, just the values.

34: This checks if there is a value for the website, if not it won’t display the title/section.

35: This checks to see if there is ‘http://’ at the front of the website address. If not then it will add it.

43: This line just shows a partial segment of the website URL, as we don’t want a 200 character long website address destroying our theme style!

That’s pretty much it! Also note the stripslashes. We put the text in with slashes, so we should remove them in order to show the information. Play around with the style, the style is pretty dark and evil at the moment, so if you like rainbow colours go style it up! Next we will get some smilies into the messages!

Posted by VoiDeT

Categorised under PHP
Bookmark the permalink or leave a trackback.

60 Comments

  1. CGar

    Hello VOIDET

    I spent last night going through this tutorial and it was great and informative.

    One question I have is that I see your guestbook example has had some spambot action. Is this because there are new techniques that your tutorial doesn’t cover? I’d like to keep this kind of crap off my guest book if possible.

    Thanks for your great tutorial and your feedback.

    Best,
    CGar

    August 3, 2009 @ 3:43 am
  2. VoiDeT

    Hey Cgar,

    This is both true and unfortunate.
    I only taught one spam catching technique.
    However more can be applied if need be. Generating a captcha form, or having an ip-ban with 30 day cool off period. Running known ip blocking from black lists.
    The honey pot technique is just one! Surprisingly, it rejects quite alot!

    Let me know if i can help you out further!
    VoiDeT

    August 3, 2009 @ 4:48 pm
  3. Dennis van Duinen

    Hi there,

    A great tut! Im trying to put it on my site.

    But there is one little problem. The honeypot.. when is add this link:

    a new text field appears on my guestbook, while you where saying that it was hidden?

    How is that possible?

    Dennis

    November 12, 2009 @ 10:40 pm
  4. Hi there. Thanks for the great tut. Sorry…forgot to read the last side, as I didn’t used all of the tut for my guestbook at the moment. So I implemented the guestbook in my website without asking you first. And…I’m not completly ready, still working on some things as the honeypot and the pagination.

    December 6, 2009 @ 8:39 am
  5. Hello VOIDET,

    This is the best tutorial on the internet so far! :D

    I’m stuck at stage 7 – 10,
    it looked fine until stage 7 then the succes-message never showed up. :(

    The IP thing didin’t work for me :/ so I jumped that part and now I’m trying to get the entries to work out, but it show me this message:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\guestbook\templates\entries.php on line 6

    do you have a idea of what’s wrong?
    I would be happy for any help, just contact my email!

    Regards Zime

    December 20, 2009 @ 11:02 pm
  6. VoiDeT

    @Zime:
    Thanks a lot for the kind words.
    That’s a shame that you can’t get the guestbook working. It looks as though your data isn’t being insert correctly. What you can do however is check your database for any records. If they aren’t in there then check what’s going on with data you’re inserting, and the insert commands.
    If you do see the data in there, then check what’s happening when you try and retrieve the records.

    I’m thinking i might rewrite this tutorial to use OOP with PHP5.
    Or maybe save that for a whole new tutorial.

    December 21, 2009 @ 1:45 pm
  7. Hi
    First great thanks to the author of this tutorial/workshop

    Its working great. But there is only a single problem with the website links in the db entrys they re not working.

    The link includes the hole file path i.e.(http://htdocs/mywebsite/www.pcsh.it) whats wrong?

    thx in advance and best regards
    Oli

    January 29, 2010 @ 4:37 am
  8. Robin

    THX a lot

    love this tut

    March 31, 2010 @ 11:22 pm
  9. ome tony

    hey, where can i find the turtorial?

    greetings

    June 1, 2010 @ 10:55 pm
  10. Dude, many thanks for this nice and clean tutorial. Took me about an hour to read it all up and add own commands.

    July 12, 2010 @ 12:13 am

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

or