Computers Windows Internet

PHP Guestbook. Writing a guestbook script Mad dealer guestbook php

PHP 5.2 and higher;
- mod_rewrite;
- Base MySQL data 4.1 and up.

Possibilities

Embedded under any site (all you need to do is edit the top.php and bottom.php files);
- anti-flood;
- ban list;
- page navigation of the view<< < ...| 5 | 6 | 7 ... > >>;
- BBCode support;
- display of emoticons;
- notification of new added messages;
- the ability to add messages for moderation;
- admin panel;
- easy to install and set up.

Installation

Unpack the archive and copy the contents of the archive to any folder on your web server (for example "guestbook"). Open config / config_db.php file in text editor and specify the connection settings (database host or IP, database name, username and password). Allocate the MySQL SQL query database tables from the guestbook.sql file. usually on most hosting this is done through the web interface via phpMyAdmin. Go to the administration panel (http: // your_site / folder_with_script / admin /) and enter the password 1111. Next, specify the necessary settings.

Commercial version

At the request of the customer, I can adapt the script for specific conditions. Various additional functions can be added.

Note

The "PHP Guestbook" SCRIPT, NEXT IS JUST THE PROGRAM IS COMPLETELY FREE. YOU MAY FREELY DISTRIBUTE, COPY, MAKE YOUR CHANGES IN THE SOURCE CODE OF THE PROGRAM, ONLY UNDER THE CONDITION OF RETAINING THE AUTHOR'S COPYRIGHT. THE USE OF THE "PHP Guestbook" SOFTWARE FOR COMMERCIAL PURPOSES IS PROHIBITED. YOUR USE OF THIS SOFTWARE IS AT YOUR OWN RISK. THE AUTHOR DOES NOT ASSUME ANY RESPONSIBILITY FOR THE PERFORMANCE OF THE PROGRAM, AS WELL AS FOR LOSSES, DAMAGE TO DATA OR ANYTHING OTHER RELATED TO THE USE AND OPERATION OF THIS PROGRAM.

If you liked my script and you have a desire to thank me with a ruble, then here are my details:

WebMoney
U237811811298
R198597198920
Z917380288657

Yandex money
41001635943434

PayPal

If you have any questions or suggestions, please write to me at: This address Email protected from spambots. You need JavaScript enabled to view it.

»» @LexGuestbook

@lex Guestbook- it's free PHP guestbook script for sites. Written in PHP language, @lex Guestbook Easy to install in minutes, making it easy for even beginners to figure it out.

PHP guestbook completely free script - the main condition: do not remove the copyright at the bottom of the page. PHP guestbook installs on your server and is completely independent of any other sites. Backup post to the PHP guestbook of your choice, even every day. Automatic installation Alex Guestbook: the names of the tables in the database are registered by themselves.

The advantages of the script.

  • Thanks to skins, you can change appearance @lexGuestbook for the design of your site. (all skins are valid XHTML 1.0 Transitional)
  • V PHP guestbook about 20 languages ​​are built-in.
  • Each country has its own flag displayed.
  • Support for emoticons: the ability to add your own emoticons, delete, edit.
  • Character statistics are displayed: you can set the maximum number of letters in one message.
  • Internal search engine.
  • Possibility of evaluation by visitors. Assessment on a 10-point system.

Supported languages

Complete translation.
French, English, Polish, German, Norwegian, Italian, Dutch, Slovak, Persian, Turkish.

Partial translation. Some words are still in English.
Spanish, Russian, Swedish, Czech, Croatian, Portuguese, Icelandic, Serbian, Danish.

PHP guestbook tutorial. Today I prepared new interesting tutorial - I will tell how you can create ajax PHP guestbook with own unique design. Our records will be saved into SQL database. This table will contain next info: name of sender, email, guestbook record, date-time of record and IP of sender. Of course, we will use jQuery too (to make it Ajax). One of important features will spam protection (we can post no more than one record every 10 minutes)!

Now - download the source files and lets start coding!

Step 1. SQL

We need to add one table to our database (to store our records):

CREATE TABLE IF NOT EXISTS `s178_guestbook` (` id` int (10) unsigned NOT NULL auto_increment, `name` varchar (255) default" ",` email` varchar (255) default "", `description` varchar (255) default "", `when` int (11) NOT NULL default" 0 ",` ip` varchar (20) default NULL, PRIMARY KEY (`id`)) ENGINE = MyISAM DEFAULT CHARSET = utf8;

Step 2. PHP

Here are source code of our main file:

guestbook.php

= ") == 1) error_reporting (E_ALL & ~ E_NOTICE & ~ E_DEPRECATED); else error_reporting (E_ALL & ~ E_NOTICE); require_once (" classes / CMySQL.php "); // including service class to work with database // get visitor IP function getVisitorIP () ($ ip = "0.0.0.0"; if ((isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])) && (! empty ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]))) ($ ip = $ _SERVER ["HTTP_X_FORWARDED_FOR"];) elseif ((isset ($ _ SERVER ["HTTP_CLIENT_IP"])) && (! Empty ($ _ SERVER ["HTTP_CLIENT_IP"]))) ($ ip = explode (".", $ _ SERVER [ "HTTP_CLIENT_IP"]); $ ip = $ ip. ".". $ Ip. ".". $ Ip. ".". $ Ip;) elseif ((! Isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])) | | (empty ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]))) (if ((! isset ($ _ SERVER ["HTTP_CLIENT_IP"])) && (empty ($ _ SERVER ["HTTP_CLIENT_IP"]))) ($ ip = $ _SERVER ["REMOTE_ADDR"];)) return $ ip;) // get last guestbook records function getLastRecords ($ iLimit = 3) ($ sRecords = ""; $ aRecords = $ GLOBALS ["MySQL"] -> getAll ("SELECT * FROM `s178_guestbook` ORDER BY` id` DESC LIMIT ($ iLimit) "); forea ch ($ aRecords as $ i => $ aInfo) ($ sWhen = date ("F j, Y H: i", $ aInfo ["when"]); $ sRecords. =<<

Record from ($ aInfo ["name"]) (($ sWhen)):

($ aInfo ["description"])

EOF; ) return $ sRecords; ) if ($ _POST) (// accepting new records $ sIp = getVisitorIP (); $ sName = $ GLOBALS ["MySQL"] -> escape (strip_tags ($ _ POST ["name"])); $ sEmail = $ GLOBALS ["MySQL"] -> escape (strip_tags ($ _ POST ["name"])); $ sDesc = $ GLOBALS ["MySQL"] -> escape (strip_tags ($ _ POST ["text"])); if ($ sName && $ sEmail && $ sDesc && $ sIp) (// spam protection $ iOldId = $ GLOBALS ["MySQL"] -> getOne ("SELECT` id` FROM `s178_guestbook` WHERE` ip` = "($ sIp)" AND `when`> = UNIX_TIMESTAMP () - 600 LIMIT 1"); if (! $ IOldId) (// allow to add comment $ GLOBALS ["MySQL"] -> res ("INSERT INTO` s178_guestbook` SET `name` = "($ sName)", `email` =" ($ sEmail) ",` description` = "($ sDesc)", `when` = UNIX_TIMESTAMP (),` ip` = "($ sIp)" ") ; // drawing last 10 records $ sOut = getLastRecords (); echo $ sOut; exit;)) echo 1; exit;) // drawing last 10 records $ sRecords = getLastRecords (); ob_start (); ?>

Guestbook Records

Add your record here

PHP guestbook | Script Tutorials

PHP guestbook

Back to original tutorial on Script Tutorials

When we open this page we will see book, at left side we will draw list of last three records, at right - form of posting new records. When we submitting form - script sending POST data (to same php page), script saving this data to database, and returning us list of fresh 3 records. Then, via fading effect we draw returned data at left column. All code contains comments - read it for better understanding code. Ok, next PHP file is:

classes / CMySQL.php

This is my own service class to work with database. This is nice class which you can use too. Database connection details located in this class in few variables, sure that you will be able to configure this to your database. I don`t will publish its sources - this is not necessary for now. Available in package.

Step 3. CSS

Now - all used CSS styles:

css / main.css

* (margin: 0; padding: 0;) body (background-color: #fff; color: #fff; font: 14px / 1.3 Arial, sans-serif;) footer (background-color: # 212121; bottom: 0; box-shadow: 0 -1px 2px # 111111; display: block; height: 70px; left: 0; position: fixed; width: 100%; z-index: 100;) footer h2 (font-size: 22px; font- weight: normal; left: 50%; margin-left: -400px; padding: 22px 0; position: absolute; width: 540px;) footer a.stuts, a.stuts: visited (border: none; text-decoration: none ; color: #fcfcfc; font-size: 14px; left: 50%; line-height: 31px; margin: 23px 0 0 110px; position: absolute; top: 0;) footer .stuts span (font-size: 22px; font-weight: bold; margin-left: 5px;) .container (background: transparent url (../ images / book_open.jpg) no-repeat top center; color: # 000000; height: 600px; margin: 20px auto; overflow: hidden; padding: 35px 100px; position: relative; width: 600px;) # col1, # col2 (float: left; margin: 0 10px; overflow: hidden; text-align: center; width: 280px;) # col1

(-webkit-transform: rotate (3deg); -moz-transform: rotate (3deg); -ms-transform: rotate (3deg); -o-transform: rotate (3deg); ) #records form (margin: 10px 0; padding: 10px; text-align: left;) #records table td.label (color: # 000; font-size: 13px; padding-right: 3px; text-align: right ;) #records table label (font-size: 12px; vertical-align: middle;) #records table td.field input, #records table td.field textarea (background-color: rgba (255, 255, 255, 0.4) ; border: 0px solid # 96A6C5; font-family: Verdana, Arial, sans-serif; font-size: 13px; margin-top: 2px; padding: 6px; width: 190px;) #records table td.field input (background -color: rgba (200, 200, 200, 0.4); cursor: pointer; float: right; width: 100px;) #records table td.field input: hover (background-color: rgba (200, 200, 200, 0.8 );) #records_list (text-align: left;) #records_list .record (border-top: 1px solid # 000000; font-size: 13px; padding: 10px;) #records_list .record: first-child (border-top -width: 0px;) #records_list .record p: first-child (font-weight: bold; font-size: 11px;)

The description and solution of this problem will be written right here, without performance checks, so there may be a typo somewhere. This was done not because of my laziness, but again because of additional training when you created the guestbook. Also, the presented solution will be a little "crooked", but more on that at the end of this article in the "Independent work" section.

If you don't know how to work with PHP at all, then first I advise you to read the PHP post - where to start.

We will analyze the creation of the most primitive guestbook, without design and administration. In other words, we will limit ourselves to entering the book, registering and leaving messages.
From the task itself, it becomes clear that we can completely limit ourselves to three php files to accomplish this task. Of course, the registration task can be divided into two: the registration form and adding data to the database, but we will not do this. The same reasoning is valid for the commenting procedure. Thus, we create three files: index.php, registration.php, book.php.

By the names of the files, you can immediately understand what they will be responsible for: index.php - the login page, registration.php - the registration page, book.php - the guest book page.

The simplest thing in our case is the login page. Since the page is responsible for entering a pair of username and password, it must have a form to send this data to the book.php page. As well as a link to the registration page. We have:

Guestbook entry

Login:
Password:

registration

A little off topic, for those who do not like what the code above means. Here we have bare HTML. Key point is the tag form which takes two parameters action, which is responsible for the address of the page that will open after confirming the data entry, and the parameter method, which answers how the form data will be sent to the specified page(see POST and GET in simple terms). Inside the form tag, fields for input (input, select) are indicated, which must have an attribute name... It is by the value of this attribute that it will be possible to get the data entered by the user on the page specified in the parameter action forms... There must be an input with the type inside the form submit, which in essence is a button, by clicking on which the data will be sent to the action page.

Let's continue ... Now we need to create a new user registration page. It is worth immediately thinking about what exactly we want to have. I decided that for a simple guestbook it would be quite enough to have users with unique logins, as well as to know the address of their Web site (if any). Once we have decided what we want to know about the user, then we can start designing our database.

Go to phpMyAdmin. Let's create a database there with the name gb. In this database, create a Users table with the following fields:

Login varchar (50) unique // username, unique field password varchar (150) // www password varchar (255) // site address

And since we have already entered the database editor, we can immediately think about how we will store messages. In our case, it is quite suitable to know who and when left the message, well, and the actual text of our message. Also, for convenience, it is worthwhile to number our messages. We have decided on this, so we can safely create another Messages table with the following structure:

Id int ai (auto_increment) primary_key // message number, counter that will increase itself mes text // text of the message who varchar (50) // username of the one who left the message when timestamp default (current) // time of leaving the message, by the default is the current date and time

With the database sorted out. Now let's move on to the registration.php file. Summarizing the above, we have:

registration

Login:
Password:
The password again:
WWW: http: //


To main

A little about the code. As you may have noticed, PHP is present here. I will not describe the form any more, but as we can see, here the form data is sent to the same page where this form is located. In other words, the registration.php file sends the data to itself. Nobody forbids doing this, however, it is not always possible to use it, since it greatly spoils the readability of the code. However, on the other hand, this clearly demonstrates the possibilities PHP language... Essentially, our page is split into two cases. The first case is when data is sent to the page with a POST request, the second is when no POST request is sent to the page. In the second case, we will show the registration form, and in the first case, we will start checking the submitted data. As you can see, the data is sent in the form of an associative array (that is, an array whose indices are strings). Moreover, the indices coincide with the names of the input fields specified in the form for sending data. Those. if we write

then we, when sending this data by POST request, will have a variable

$ _POST ["vasa-peta"]

It is also worth noting that we do not do any checks for repeated login. This check will be carried out at the database level, since the login field is marked as unique.

Now the last one. It remains to make the guestbook itself. A bit of reasoning. Since the guestbook works with a username and password, the first thing we have to do is check the entered username and password for availability in the database. Since we decided to add messages in the book.php file as well, the procedure for adding a message should obviously follow. Well, at the very end, the display of all messages. (if you add a message after they are displayed, the added message will be displayed only after the page is reloaded). We have:

Guest book ";) else (echo" Your message has not been added!


"; } } ?>
"name =" login "/> "name =" password "/>

"; echo" Text: ". $ row [" mes "]."
";)) mysql_close ($ link);?>

That's essentially all. If the code is 100% working, good. If not, then I advise you to try to correct the mistakes yourself. Now the promised section.

Independent work
As mentioned at the beginning, this solution is "crooked". If you still do not understand why, then I will explain now. As you may have noticed in the book.php file, there are several hidden fields in the form for adding a comment, and two of them are the username and password entered by the user. Not good, is it? Try to fix this issue using sessions (How to work with sessions in PHP).
Try to answer the question, how are functions different? empty / isset and exit / die?
Change the request for retrieving messages from the database so that when messages are displayed, the website of the author of the message is displayed.
If you have noticed, the password of users in the database is stored in clear text, which is not very good. Edit the code so that the hash of the user's password is stored in the database (for example, MD5 or SHA1).

In this tutorial, we will create a PHP guestbook using AJAX. The records will be stored in the database. The table will contain the following information: sender's name, email address, IP address and date-time of the last entry. JQuery will be used (for AJAX implementation). A simple spam protection will also be implemented - you can post no more than one entry every 10 minutes.

Step 1. SQL

For our application to work, we need to create a table:

CREATE TABLE IF NOT EXISTS `s178_guestbook` (` id` int (10) unsigned NOT NULL auto_increment, `name` varchar (255) default" ",` email` varchar (255) default "", `description` varchar (255) default "", `when` int (11) NOT NULL default" 0 ",` ip` varchar (20) default NULL, PRIMARY KEY (`id`)) ENGINE = MyISAM DEFAULT CHARSET = utf8;

Step 2. PHP

The main file will contain the following code:

guestbook.php

= ") == 1) error_reporting (E_ALL & ~ E_NOTICE & ~ E_DEPRECATED); else error_reporting (E_ALL & ~ E_NOTICE); require_once (" classes / CMySQL.php "); // including service class to work with database // Get Visitor IP function getVisitorIP () ($ ip = "0.0.0.0"; if ((isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])) && (! Empty ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]))) ($ ip = $ _SERVER ["HTTP_X_FORWARDED_FOR"];) elseif ((isset ($ _ SERVER ["HTTP_CLIENT_IP"])) && (! Empty ($ _ SERVER ["HTTP_CLIENT_IP"]))) ($ ip = explode (".", $ _ SERVER [ "HTTP_CLIENT_IP"]); $ ip = $ ip. ".". $ Ip. ".". $ Ip. ".". $ Ip;) elseif ((! Isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])) | | (empty ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]))) (if ((! isset ($ _ SERVER ["HTTP_CLIENT_IP"])) && (empty ($ _ SERVER ["HTTP_CLIENT_IP"]))) ($ ip = $ _SERVER ["REMOTE_ADDR"];)) return $ ip;) // Get the last entries in the guestbook function getLastRecords ($ iLimit = 3) ($ sRecords = ""; $ aRecords = $ GLOBALS ["MySQL"] -> getAll ( "SELECT * FROM` s178_guestbook` ORDER BY `id` DESC L IMIT ($ iLimit) "); foreach ($ aRecords as $ i => $ aInfo) ($ sWhen = date ("F j, Y H: i", $ aInfo ["when"]); $ sRecords. =<<

($ aInfo ["description"])

EOF; ) return $ sRecords; ) if ($ _POST) (// Accept new entries $ sIp = getVisitorIP (); $ sName = $ GLOBALS ["MySQL"] -> escape (strip_tags ($ _ POST ["name"])); $ sEmail = $ GLOBALS ["MySQL"] -> escape (strip_tags ($ _ POST ["name"])); $ sDesc = $ GLOBALS ["MySQL"] -> escape (strip_tags ($ _ POST ["text"])); if ($ sName && $ sEmail && $ sDesc && $ sIp) (// Anti-spam $ iOldId = $ GLOBALS ["MySQL"] -> getOne ("SELECT` id` FROM `s178_guestbook` WHERE` ip` = "($ sIp) "AND` when`> = UNIX_TIMESTAMP () - 600 LIMIT 1 "); if (! $ IOldId) (// You can add a comment $ GLOBALS [" MySQL "] -> res (" INSERT INTO `s178_guestbook` SET` name` = "($ sName)", `email` =" ($ sEmail) ",` description` = "($ sDesc)", `when` = UNIX_TIMESTAMP (),` ip` = "($ sIp)" ") ; // Print the last 10 records $ sOut = getLastRecords (); echo $ sOut; exit;)) echo 1; exit;) // Print the last 10 records $ sRecords = getLastRecords (); ob_start (); ?>

Guest book

Add your review here

PHP Guestbook

When you open the page, you will see a notebook. The last three entries are displayed on the left side, and a form for posting a new review is displayed on the right. When the form is submitted, the script sends the POST data (to the same PHP page), then stores the received data in the database and returns the last three records. Using the development effect, the entries are displayed on the left side of the book.

Also used PHP file

classes / CMySQL.php

This file contains the class code for working with the database. It requires three variables to be configured to connect to the server.

$ this-> sDbName = "DATABASE_NAME"; $ this-> sDbUser = "USER NAME"; $ this-> sDbPass = "PASSWORD";

Step 3. CSS

And now let's form the appearance of the guestbook:

css / main.css

* (margin: 0; padding: 0;) body (background-color: #fff; color: #fff; font: 14px / 1.3 Arial, sans-serif;) footer (background-color: # 212121; bottom: 0; box-shadow: 0 -1px 2px # 111111; display: block; height: 70px; left: 0; position: fixed; width: 100%; z-index: 100;) footer h2 (font-size: 22px; font- weight: normal; left: 50%; margin-left: -400px; padding: 22px 0; position: absolute; width: 540px;) footer a.stuts, a.stuts: visited (border: none; text-decoration: none ; color: #fcfcfc; font-size: 14px; left: 50%; line-height: 31px; margin: 23px 0 0 110px; position: absolute; top: 0;) footer .stuts span (font-size: 22px; font-weight: bold; margin-left: 5px;) .container (background: transparent url (../ images / book_open.jpg) no-repeat top center; color: # 000000; height: 600px; margin: 20px auto; overflow: hidden; padding: 35px 100px; position: relative; width: 600px;) # col1, # col2 (float: left; margin: 0 10px; overflow: hidden; text-align: center; width: 280px;) # col1