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

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13

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:

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

Add:

  1. <tr>
  2.     <td height="10"></td>
  3.   </tr>
  4.   <tr>
  5.     <td valign="top"><?php include('entries.php'); ?></td>
  6.   </tr>

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

  1. <?php
  2.   $query = mysql_query("SELECT *, UNIX_TIMESTAMP(`date`) as date FROM `entries` ORDER BY `date` DESC");
  3. ?>
  4. <table width="100%" cellpadding="0" cellspacing="0" border="0">
  5.   <?php 
  6.     while($row = mysql_fetch_array($query)){
  7.     $i++;
  8.     if(($i % 2) == 0){
  9.       $class = 'class="entry1"';
  10.       $classbg = 'class="entriesbg1"';
  11.     } else {
  12.       $class = 'class="entry2"';
  13.       $classbg = 'class="entriesbg2"';
  14.     }
  15.   ?>
  16.   <tr>
  17.     <td valign="top" <?php echo $classbg; ?>>
  18.     <table width="100%" cellpadding="0" cellspacing="0" border="0">
  19.       <tr>
  20.         <td valign="top" align="right">#<?php echo $row['id']; ?> Posted on: <?php echo date("d/m/y g:i a", $row['date']); ?></td>
  21.       </tr>
  22.       <tr>
  23.         <td valign="top">
  24.         <table width="100%" cellpadding="0" cellspacing="0" border="0" <?php echo $class; ?>>
  25.           <tr>
  26.             <td valign="top"><span>Name:</span></td>
  27.             <td valign="bottom" width="100%"><?php echo stripslashes($row['name']); ?></td>
  28.           </tr>
  29.           <tr>
  30.             <td valign="top" nowrap="nowrap"><span>E-Mail:</span></td>
  31.             <td valign="bottom" width="100%"><?php echo stripslashes($row['email']); ?></td>
  32.           </tr>
  33.           <?php
  34.             if(strlen($row['website']) > 0){
  35.               if(substr(strtolower($row['website']), 0, 7) != 'http://'){
  36.                 $website = 'http://'.$row['website'];
  37.               } else {
  38.                 $website = $row['website'];
  39.               }
  40.           ?>
  41.           <tr>
  42.             <td valign="top" nowrap="nowrap"><span>Website:</span></td>
  43.             <td valign="bottom" width="100%"><a href="<?php echo stripslashes($website); ?>" target="_blank"><?php echo substr($website,0,30); ?></a></td>
  44.           </tr>
  45.           <?php
  46.             }
  47.           ?>
  48.           <tr>
  49.             <td valign="top" colspan="2"><span>Message:</span></td>
  50.           </tr>
  51.           <tr>
  52.             <td valign="top" colspan="2"><?php echo stripslashes($row['message']); ?></td>
  53.           </tr>
  54.         </table>
  55.         </td>
  56.       </tr>
  57.     </table>
  58.     </td>
  59.   </tr>
  60.   <tr>
  61.     <td valign="top" height="10"></td>
  62.   </tr>
  63.   <?php } ?>
  64. </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:

  1.  
  2. .entries {
  3.   padding: 10px 10px 0px 10px;
  4.   border: #999999 solid 1px;
  5.   font-size: 14px;
  6.   background: #333333;
  7. }
  8.  
  9. .entriesbg2 {
  10.   padding: 10px;
  11.   border: #999999 solid 1px;
  12.   font-size: 14px;
  13.   background: #111111;
  14. }
  15.  
  16. .entriesbg1 {
  17.   padding: 10px;
  18.   border: #999999 solid 1px;
  19.   font-size: 14px;
  20.   background: #555555;
  21. }
  22.  
  23. .entrytitle {
  24.   font-family: Helvetica, Arial, Verdana, Courier;
  25. }
  26.  
  27. .entrysmall {
  28.   font-size: 12px;
  29. }
  30.  
  31. .entry1 {
  32.   padding: 5px;
  33.   margin: 2px;
  34.   border: #222222 solid 1px;
  35.   background: url('../images/entry2.jpg') repeat-x #000000;
  36. }
  37.  
  38. .entry2 {
  39.   padding: 5px;
  40.   margin: 2px;
  41.   border: #555555 solid 1px;
  42.   background: url('../images/entry1.jpg') repeat-x #333333;
  43. }
  44.  
  45. a {
  46.   color: #FFFFFF;
  47.   text-decoration: none;
  48. }
  49.  
  50. a:hover {
  51.   text-decoration: underline;
  52. }
  53.  

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 3x5 = 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!

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13

Tags
, , , , , , , ,

  1. 32 Responses to “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”

  2. By VLADIS SLOVAKIA Windows XP Mozilla Firefox 2.0.0.14 on Jun 2, 2008

    I make this guestbook.
    It go not me. It always write this errors:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /3w/wz.cz/m/medvede/5/includes/actions.php on line 3

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /3w/wz.cz/m/medvede/5/includes/functions.php on line 12

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /3w/wz.cz/m/medvede/5/templates/entries.php on line 15

    i have it on http://medvede.wz.cz/5/index.php

    Help me, please.
    thank you

  3. By VoiDeT AUSTRALIA Mac OS X Safari 525.20 on Jun 2, 2008

    Hey Vladis,

    Did you make sure you have created the database correctly?

    Please make sure you have done this, otherwise this error would definitely show up.

  4. By VLADIS SLOVAKIA Windows XP Mozilla Firefox 2.0.0.14 on Jun 3, 2008

    I make this TABLE :

    CREATE TABLE `entries` (
    `id` int(8) NOT NULL auto_increment,
    `name` varchar(255) collate latin1_general_ci NOT NULL,
    `email` varchar(255) collate latin1_general_ci NOT NULL,
    `website` varchar(255) collate latin1_general_ci NOT NULL,
    `message` text collate latin1_general_ci NOT NULL,
    `date` timestamp NOT NULL default CURRENT_TIMESTAMP,
    `ip` varchar(15) collate latin1_general_ci NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

    and this:

    CREATE TABLE `spam` (
    `id` int(8) NOT NULL auto_increment,
    `ip` varchar(15) collate latin1_general_ci NOT NULL,
    PRIMARY KEY (`id`),
    KEY `ip` (`ip`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

    I have in config.php this code:

    And it doesn’t go.

  5. By VLADIS SLOVAKIA Windows XP Mozilla Firefox 2.0.0.14 on Jun 3, 2008

    I have in config.php this code:

    $host = ‘mysql.webzdarma.cz’;
    $username = ‘medvede48′;
    $password = ‘xxx’;
    $dbname = ‘guestbook’; - - I try also entries
    $email = ‘your@email.com’;
    $connect = mysql_connect($host, $username, $password);
    $dbselect = mysql_select_db($dbname);
    $items = 10;

  6. By VoiDeT AUSTRALIA Mac OS X Safari 525.20 on Jun 3, 2008

    Can you please provide me with your ftp details?
    It could be my end, or it could be your end. But i thought i tested this script without any rows in the database. Let me know

  7. By VLADIS SLOVAKIA Windows XP Mozilla Firefox 2.0.0.14 on Jun 3, 2008

    I have it on: photoshopsk.wz.cz
    password: 7754705

    I have files from this tutorial.

  8. By VoiDeT AUSTRALIA Mac OS X Safari 525.20 on Jun 4, 2008

    And your username for me to log in please?

  9. By VLADIS SLOVAKIA Windows XP Mozilla Firefox 2.0.0.14 on Jun 5, 2008

    (https://www.webzdarma.cz/)
    My username on FTP is : photoshopsk.wz.cz
    and password: 7754705

    (https://www.webzdarma.cz/mysql/index.php)
    And username on mysql server is: photoshopsk
    password:ragp3s

  10. By VoiDeT AUSTRALIA Mac OS X Safari 525.20 on Jun 7, 2008

    Those settings do not work.

    I need username, password, and address.

    Otherwise i cannot look for you.

  11. By VLADIS SLOVAKIA Windows XP Mozilla Firefox 2.0.0.14 on Jun 7, 2008

    Look you:
    1) http://photoshopsk.wz.cz/1/1.JPG
    2) http://photoshopsk.wz.cz/1/2.JPG
    3) http://photoshopsk.wz.cz/1/3.JPG

    Do you thing this or no?
    If no this, then what you think? What of address?

  12. By Linnea SWEDEN Windows XP Internet Explorer 7.0 on Jun 9, 2008

    Hi! I just want to say thank you for a wonderful tutorial. I will probably use this at my website when I have finished it, so I can send the link later. Thank you!

  13. By VoiDeT AUSTRALIA Mac OS X Safari 525.20 on Jun 9, 2008

    @ Linnea - Thanks alot for your comment. I would love to see your website when you have finished with it!

    @Vladis - Doesn’t work dude. Maybe you have limited the IP range of access?

  14. By VLADIS SLOVAKIA Windows XP Mozilla Firefox 2.0.0.14 on Jun 11, 2008

    My action what I make.

    1.) I am download this tutorial: http://www.jotlab.com/wp-content/uploads/2008/04/guestbook.zip

    2.) I give it on a web all. (http://photoshopsk.wz.cz/)

    3.) I am create table :

    CREATE TABLE `entries` (
    `id` int(8) NOT NULL auto_increment,
    `name` varchar(255) collate latin1_general_ci NOT NULL,
    `email` varchar(255) collate latin1_general_ci NOT NULL,
    `website` varchar(255) collate latin1_general_ci NOT NULL,
    `message` text collate latin1_general_ci NOT NULL,
    `date` timestamp NOT NULL default CURRENT_TIMESTAMP,
    `ip` varchar(15) collate latin1_general_ci NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

    and this:

    CREATE TABLE `spam` (
    `id` int(8) NOT NULL auto_increment,
    `ip` varchar(15) collate latin1_general_ci NOT NULL,
    PRIMARY KEY (`id`),
    KEY `ip` (`ip`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

    4.) I chanqe in config.php on it :

    AND IT NO GO.
    You know where is mistake???

  15. By Amanda UNITED STATES Windows XP Internet Explorer 6.0 on Jun 11, 2008

    Hello. I am trying to make a wedding website and want to add a guestbook feature. Everything seemed to be working okay but now I get two big errors.

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/.magdalen/danfrancis/www/attempt/includes/actions.php on line 3

    and

    Fatal error: Call to undefined function: pagination() in /home/.magdalen/danfrancis/www/attempt/templates/skin.php on line 48

    Any clue what is going on? Any help would be great. The site it’s at right now (just testing it out) is: http://www.danfrancisphotography.com/attempt/index.php

    THANKS AGAIN!

  16. By Lily DENMARK Windows XP Internet Explorer 7.0 on Jun 11, 2008

    Hello there.

    It doesn’t work at myhomepage.. Can you please tell me, what I’ve done wrong?

    - Lily.

  17. By VoiDeT AUSTRALIA Mac OS X Safari 525.20 on Jun 11, 2008

    Hey people,

    I don’t know why you are having these problems. It sounds like an error in the SQL. I am happy to look on your server if you provide me with the correct FTP details or cpanel details.

    I have installed this script from the zip file and it works fine.

    Thank you

  18. By Lily DENMARK Windows XP Internet Explorer 7.0 on Jun 12, 2008

    What do you mean with the correct FTP details or cpanel details?

  19. By VoiDeT AUSTRALIA Mac OS X Safari 525.20 on Jun 12, 2008

    However you upload the files to your server,
    so i can see what the problem is. Because i cant replicate it

  20. By Lily DENMARK Windows XP Internet Explorer 7.0 on Jun 12, 2008

    The only thing I’ve changed is the MySQL otherwise I haven’t touched anything. The same text as Amanda got I have at my page.

  21. By VoiDeT AUSTRALIA Mac OS X Safari 525.20 on Jun 12, 2008

    Yep,
    what sql did you change?
    the connection settings?

  22. By eHobayyeb SAUDI ARABIA Windows Vista Mozilla Firefox 3.0 on Jun 22, 2008

    Amazing!

    Everything works fine.
    I am new PHPier and found many useful tips & tricks!

    Keep it up VoiDeT, I will do all PHP tuts here.

    Thanks
    Mohammad
    hattoon.com

  23. By HCF GERMANY Windows Vista Mozilla Firefox 3.0 on Jul 5, 2008

    Hi, awesome tutorial, shows exactly how to use the basics. 2 questions regarding your techniques:
    1. What about using mySQLi instead of the usual mySQL (only PHP5, but way better), since it is faster and more secure.
    2. I guess this was designed for beginner and advanced user, so it would be useful to show a lil bit of object oriented programming, since it makes the source code more accessible and php more flexible.

    Awesome work, greetings from Germany.

  24. By VoiDeT AUSTRALIA Mac OS X Safari 525.20 on Jul 5, 2008

    HCF!

    Vielen dank für ihre nette antwort. Du hast auf Englisch geschreiben, so ich werde auf Deutsch antworten. Es freut mich so viel das du die tutorial magst. So danke noch mal. Ich hab noch nicht viele mysqli probiert. Aber ich weiß das es OOP ist. Für dieser tutorial will ich sehr einfach machen, aber vielleicht lern ich MySQLi für meine selber projekte.

    Vielen dank noch mal,

  25. By Fredrik Windows XP Flock 1.2.1 on Jul 7, 2008

    Hi voidet,

    I like the icons you use to identify the country, OS and browser. How did you do that?

  26. By VoiDeT AUSTRALIA Mac OS X Safari 525.20.1 on Jul 7, 2008

    Firestats plugin mate ;)

  27. By Akira SWEDEN Windows XP Mozilla Firefox 3.0.1 on Jul 30, 2008

    Nice guestbook. I like it. :)

  28. By Dan Bunyard UNITED STATES Windows XP Mozilla Firefox 3.0.1 on Aug 9, 2008

    Awesome work! I have a guestbook on my existing site but….to put it mildly….it was hacked together to make it work. You have sure a great example here of the things you can do with PHP/MySQL. Keep up the great work, and happy coding!!

  29. By Franklyn CANADA Windows XP Mozilla Firefox 3.0.1 on Aug 11, 2008

    I havent read the code in detail but wouldnt it be better to do the error handling client side ?.

  30. By VoiDeT AUSTRALIA Mac OS X Safari 525.20.1 on Aug 12, 2008

    Doing just client side opens you up to attacks.
    This is a server side tutorial anyhow. By just doing client side error checking would be insecure and negligent.

  31. By mm UNITED STATES Windows Vista Internet Explorer 7.0 on Aug 24, 2008

    hi - this is really a great tutorial. Im very glad you did this as I was confused about how to add a honeypot.

    listen, do you know how to add an ADMIN page/function to this GB, or is it already there? I was testing mine and managed to ban my own IP! I’m not sure how to manage the database at this point. Any help is appreciated.
    Thanks!

  32. By VoiDeT AUSTRALIA Mac OS X Safari 525.20.1 on Aug 24, 2008

    Hey,

    Simply go into phpmyadmin and delete the entry in the banlist that is associated with your IP address :)

    Thanks for the comments!

  33. By Yousha ISLAMIC REPUBLIC OF IRAN Windows XP Opera 9.27 on Sep 11, 2008

    This form needs CAPTCHA security image.
    GL.

Post a Comment