PHP scripts. How to manage messages from the guestbook Phoca Guestbook Non-refundable guestbook htm

A large number of articles have been written on the topic of creating guest books, but most of them revealed the writing process not completely, but partially. In addition, the books written were as simple as possible, and could not do anything other than add messages. In this article I will try to describe in detail the process of creating a guest book with a good set of functions. The guest book is written in PHP using text files for data storage.

So, first of all, we need to deal with the files. We will have four of them, although we can combine them into one, but it’s up to you to decide...

File No. 1 - guestbook.inc.dat

Let's create it, leave it empty, and give it permissions (0777-*nix, rwx/rwx/rwx)

All messages will be recorded here... Yes, yes!!! Exactly in one file! But don't be alarmed. I personally checked it on the well-known WallSt server. database sizes of 5, 10, 100, 250, 500, 1000 KB! There were slowdowns only on a 1000 KB book, believe me, this is more than 3000 average records! You will have more than enough of it.

File No. 2 – config.inc

File No. 3 – gb.php

Guestbook

*Name:
Email:
*Message:

$c-1)break; list($name,$email,$date,$time,$guesttext)=split(":::","$data[$i]", 5); /* I deliberately did not make super_mega_sophisticated designs, I will just limit myself to simple text output! Anyone who needs it will easily understand what needs to be done! For those who don’t understand, the soap will be lower, so read on... */ echo " $name@[$date, $time]
$guesttext

"; ) $col=$c/$gb_on_page; echo "
"; if ($c<$gb_on_page){ echo ""; } else { echo "Pages:"; /* There is such a thing here that if you can go to the previous page, then something like this is shown :)<< 2 */ if($c>$gb_on_page & $page>0)( echo "<<"; } /* Ниже функция, которая отвечает за вывод нумерации страниц с сообщениями… */ for($i=0;$i<$col;$i++) { if(($i)!=$page)echo "[".($i+1)."] "; else echo "".($i+1).""." "; ) ) /* There is such a thing that if you can go to the next page, then something like this is shown :) 1 >>*/ if($c>$gb_on_page & $page<$col-1){ echo ">>"; ) echo "
Total messages: $c"; echo "
"; ?>

File No. 4 - gbadd.php

", ">", $guesttext); $name = ereg_replace("<", "<", $name); $name = ereg_replace(">", ">", $name); $email = ereg_replace("<", "<", $email); $email = ereg_replace(">", ">", $email); // no quotes! 2=DeaD= thanX! $name = ereg_replace(""", """, $name); $email = ereg_replace(""", """ , $email); $guesttext = ereg_replace(""", """, $guesttext); // removing extra spaces // in the center $guesttext=ereg_replace(" +"," ",$guesttext); $name= ereg_replace(" +"," ",$name); $email=ereg_replace(" +"," ",$email); //right and left $name=trim($name); $email=trim($email ); $guesttext=trim($guesttext); // newline $guesttext = ereg_replace("n", "
", $guesttext); // so that all the text is on one line! $guesttext = ereg_replace("r", "", $guesttext); // megaquotes $name = stripslashes($name); $email = stripslashes($ email); $guesttext= stripslashes($guesttext); // correct dashes $guesttext = preg_replace ("/(1,)-(1,)/", " - ", $guesttext); $guesttext = preg_replace ("/ ^- /", "- ", $guesttext); // number-number $guesttext = preg_replace ("/(d)-(d)/", "\1–\2", $guesttext); // words with a hyphen $guesttext = preg_replace ("/(S+)-(S+)/", " \1-\2", $guesttext); // particles and prepositions $guesttext = preg_replace ("/([А-Яа-яA-Za-z]) (li|l|zhe|f|by|b)([^A-Zaa -яA-Za-z])/", "\1 \2\3", $guesttext); $guesttext = preg_replace ("/(s)([А-Яа-я](1))s/", "\1\2 ", $guesttext); // A.S. Pushkin $guesttext = preg_replace ("/([A-ZA-Z])([. ](1))(0,1)([A-ZA- Z])([. ](1))(0,1)([A-Za-Z][A-Za-zA-Za-z]*)/", "\1. \3. \5" , $guesttext); // Pushkin A. S. $guesttext = preg_replace ("/([A-Za-Z][A-Za-z]*) ([A-Za-Z])[ . ](1)(0,1)([A-ZA-Z]).([,)](1))/", "\1 \2. \3.\4", $guesttext); // convert addresses into links! $guesttext = eregi_replace("([_a-z0-9-]+(.[_a-z0-9-]+)*@+( .+)*(.(2,3)))", "\0", $guesttext); $guesttext = eregi_replace("((ht|f)tp://www.|www.)(+(. +)*(.(2,3))((/|?)*)*)", "http://www.\3", $guesttext); $guesttext = eregi_replace("((ht|f) tp://)(((+(.+)*(.(2,3)))|(((1,3).)(3)((1,3))))((/|? )*)*)", "\0", $guesttext); // cut off too large a piece of information $name=substr($name,0,$name_max_size); $email=substr($email,0, $email_max_size); $guesttext=substr($guesttext,0,$message_max_size); // BBcodes $guesttext = ereg_replace("[b]", " ", $guesttext); $guesttext = ereg_replace("", "", $guesttext); $guesttext = ereg_replace("[i]", " ", $guesttext); $guesttext = ereg_replace("", "", $guesttext); $guesttext = ereg_replace("[u]", " ", $guesttext); $guesttext = ereg_replace("", "", $guesttext); $guesttext = ereg_replace("", " ", $guesttext); $guesttext = ereg_replace("", "", $guesttext); $guesttext = ereg_replace("", " ", $guesttext); $guesttext = ereg_replace("", "", $guesttext); $guesttext = ereg_replace("", " ", $guesttext); $guesttext = ereg_replace("", "", $guesttext); $guesttext = ereg_replace("", " ", $guesttext); $guesttext = ereg_replace("", "", $guesttext); // send a message to my email // message $message .="".$date.", ".$time." n"; $message .="Name: ".$name.", e-mail: ".$email."nMessagen"; $message .="".$guesttext."n "; $message .="- ---------rn"; $message .="Message from the guest book"; // headers $headers .="From: $site_name<$recipient>n"; $headers .="X-Mailer: Pseon’s Guestbookn"; $headers .="X-Priority: 1n"; $headers .="Content-Type: text/plain; charset=windows-1251n"; // send everything to hell! mail($recipient, $subject, $message, $headers); $msg="$name:::$email:::$date:::$time: ::$guesttext "; $lines = file("guestbook.inc.dat"); $fp = fopen("guestbook.inc.dat", "w"); fwrite($fp, "$msgn"); for ($i = 0; $i< count($lines); $i++) { @fwrite($fp, "$lines[$i]"); } fclose($fp); clearstatcache(); } // если не удалось, отсылаем его обратно! else { header("Location: gb.php"); } ?>

Well, now the banana one :) For a snack, I want to offer you a set of BB-codes that will help your visitors somewhat diversify the messages in your guest books :)

» » @lexGuestbook

@lexGuestbook- it's free PHP guestbook script for websites. Written in PHP language, @lexGuestbook Easy to install in minutes, even beginners will find it easy to understand.

PHP guestbook A completely free script - the main condition: do not remove the copyright at the bottom of the page. PHP guestbook is installed on your server and is completely independent from any other sites. Backup a message in the PHP guestbook at your request, even every day. Automatic installation Alex Guestbook: the names of the tables in the database are written themselves.

Benefits of the script.

  • Thanks to skins, you can change the appearance @lexGuestbook to suit your website design. (all skins are valid XHTML 1.0 Transitional)
  • IN PHP guestbook About 20 languages ​​are built-in.
  • Each country has its own flag.
  • 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. Score on a 10-point system.

Supported languages

Full 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.

Today I will describe perhaps the best task for beginners of the PHP programming language - Guest Book. In my opinion, this task is the simplest, but at the same time it allows you to learn all the main features of working with PHP and the MySQL database. In addition, this task is infinitely expandable, which allows you to hone new technologies.

The description and solution to this problem will be written right here, without testing for functionality, so perhaps there is a typo somewhere. This was done not because of my laziness, but again because of additional training when creating a guest book by you. 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 article PHP - where to start.

We will analyze the creation of the most primitive guest book, without design and administration. In other words, we will limit ourselves to logging into the book, registering and leaving messages.
From the task itself, it becomes clear that we can completely limit ourselves to three PHP files to implement 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. Similar reasoning is valid for the procedure for leaving comments. 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 - login page, registration.php - registration page, book.php - guest book page.

The simplest thing in our case is the login page. Since the page is responsible for entering a login and password pair, it must contain a form for sending this data to the book.php page. And also a link to the registration page. We have:

Login to the guest book

Login:
Password:

Registration

Moving a little away from the topic, for those who don’t know what the code above means. Here we have bare HTML. The key 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 words). Inside the form tag, input fields (input, select) are indicated, which must have the attribute name. It is by the value of this attribute that it will be possible to obtain the data entered by the user on the page specified in the action form parameter. There must be an input with the type inside the form submit, which is essentially a button, clicking on which will send data to the action page.

Let's continue... Now we need to create a new user registration page. It’s worth immediately thinking about what exactly we want to have. I decided that for a simple guest book it would be enough to have users with unique logins, as well as to know the address of their Web site (if there is one). 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, we will create a table Users with the following fields:

Login varchar(50) unique //user login, unique field password varchar(150) //password www varchar(255) //website 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 left the message and when, and the actual text of our message. Also, for convenience, it is worth numbering our messages. We have decided on this, which means we can safely create another Messages table with the following structure:

Id int ai(auto_increment) primary_key //message number, counter, which itself will increase mes text //message text who varchar(50) //login of the person who left the message when timestamp default(current) //time of leaving the message, according default current date and time

We've sorted out the database. 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 have already noticed, PHP is present here. I won’t describe the form anymore, 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 data to itself. Nobody forbids doing this, but you can’t always use it, as it greatly spoils the readability of the code. However, on the other hand, this clearly demonstrates the capabilities of the PHP language. Essentially our page is divided into two cases. The first case is when data is sent to the page by a POST request, the second case 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 begin to check the submitted data. As you can see, the data is sent in the form of an associative array (i.e., an array whose indices are strings). Moreover, the indices coincide with the names of the input fields specified in the data submission form. Those. if we write

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

$_POST["vasa-peta"]

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

Now the last thing. All that remains is to make the guest book itself. A little discussion. Since the guest book works using a login and password, the first thing we have to do is check the entered login and password for availability in the database. Since we decided to add messages in the book.php file, then obviously the procedure for adding a message should follow. Well, at the very end, a 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 errors yourself. Now the promised section.

Independent work
As mentioned at the beginning, this solution is “crooked”. If you still don’t understand why, then I’ll explain now. As you may have noticed in the book.php file in the form for adding a comment there are several hidden fields, two of them being the username and password entered by the user. Not good, is it? Try to correct this defect using sessions (How to work with sessions in PHP).
Try to answer the question of how the functions differ empty/isset And exit/die?
Change the query for selecting messages from the database so that when displaying messages, the website of the message author is displayed.
If you noticed, the user password in the database is stored in clear text, and this is not very good. Edit the code so that a hash of the user's password (for example MD5 or SHA1) is stored in the database.

Creating a guest book on the site

In previous articles, we looked at creating a feedback form on a website. Another option for two-way communication with visitors is Guest book, in which visitors leave messages directly on the site page. The site administrator can respond to them, edit, delete, etc. Guest book Despite its undoubted advantages over the feedback form, it has one big drawback - it adds worries to the administrator. Now all messages from visitors are visible on the site and sometimes you need to respond to them. In addition, our guests write not only grateful reviews, but also all sorts of obscene things, and they do this with special zeal. I would kill these "writers"! Therefore, the guest book must be constantly monitored and education of hooligans and slackers must be done. An example of a guest book is shown in Fig. 1:



Fig.1. One of the guest book options

There are many options for guest books, paid and free, on the Internet. Sometimes a guest book is included in the list of services provided by hosting. In this case, you just need to place a link to the guest book on a page of your website and, if desired, configure its interface through the control panel. One problem is that when you change hosting, you will lose your guest book. So it’s better to have your own, especially since installing it on the site is as easy as two.

Guest books can be built using databases or without them. In the latter case, the information is stored in a regular file. If you are a supporter of simple solutions, then I recommend the second option, that is, without creating a database. I liked the guest book developed by Sergei Shestopalov (see his website "Home Scripts"). The site also offers interesting author's developments for interactive communication with visitors: forums, guest books, various forms for comments, sending messages, collecting statistics and many other useful things. Be sure to check it out, I recommend it!

For example, consider the installation Guest book version 2.0. It provides the following functions:

  • message management: deleting, editing, commenting,
  • setup and all work is done through the control panel, that is, you do not need knowledge of programming and other intricacies,
  • messages support emoticons, text and color formatting,
  • message preview,
  • there is an Antispam, Anti-Hacker system, blocking users by IP, searching for an attacker by IP,
  • Advertising protection and security are organized using captcha (input of distorted numbers). You can also create a “black list” of unwanted visitors and a list of prohibited words,
  • fully customizable interface and much more.

Guestbook 2.0 Despite its simplicity, it is quite sufficient for most applications. You can download it for free from the corresponding page of Sergei Shestopalov’s website. Size of the folder with files after unpacking (name - Book) about 400 KB.

Let's start installing a guest book on your website:

1. First, you need to check that your hosting plan (the office where your website is hosted) supports PHP. If not, then you will most likely have to pay extra to switch to another tariff that supports PHP.

2. Uploading the folder Book to your website, usually in the root directory, through the control panel or via FTP .

3. We place a hyperlink to the guest book on the desired page of the site - http://www.your_site /book/index.php.

4. It is also advisable to check the access rights to files and folders on your site. The most convenient way to do this is through the control panel. Next to each folder and file there are icons like rwx, r-x or numbers 755, 644, etc. They just indicate access rights for the site administrator and guests:
r(or 4) - the right to read data from the file,
w(or 2) - the right to change the contents of the file,
x(or 1) - the right to execute the file.

Digital coding is used to shorten the recording. For example, 7 is equivalent to writing rwx (4+2+1), etc. Without going into detail about these abbreviations, since they are usually explained in the site control panel manual, check that your folders are set to 755 (which corresponds to rwx, r-x, r-x), and your files are 644 (rw-, r--, r--). Usually these values ​​are set by default and you won’t have to change anything, but it’s best to check. As they say, “my neighbor is my friend, but it’s better to hide the lard.”

That's all! Go to your website, open the page on which you placed a link to the guest book, follow this link and leave the first message to your loved one.

Now a few words about guest book maintenance. The administrator's session with the guest book begins with authorization - logging into the control panel at http://www.your_site /book/admin/index.php. Login and password for the first login admin 111.

Next, you can change your password (it is advisable to do this) and start editing posts and replying to them. The admin panel interface is extremely friendly and you can easily figure out all the functions. All tools are clear and additionally provided with tips. For example, Fig. 2 shows the panel for customizing the appearance of the main page of the guest book and reveals the list of available panels:



Fig.2.

Of course, in exactly the same way as described, you can install a more complete version Guest book 3.2 Sergei Shestopalov, which has some additional functions: a statistics module, the ability to check messages by the administrator before publishing on the site (moderation) and some others.

As I already said, on the pages guest book Various unwanted “friends” like to leave their messages. They usually post links to their sites here, hiding behind rave reviews about your site. In 99% of cases it is spam. Therefore, I recommend that you exclude entering the website address in messages. To do this, log into the guest book control panel and on the tab Appearance - Shape Uncheck the "Site" item to prevent spammers from using the guest book to post links to their sites. It is better to leave email and ICQ, as sometimes people want to receive a confidential answer, that is, without posting in the guest book. When finished, don't forget to click the "Save" button at the bottom of the page.

Removing the "Site" item does not always help against clogging the guest book and it is best to check messages before posting them. By the way, if you receive few messages, you can simplify the guest book to a regular HTML page. This simplest option is described in

– guest book component for Joomla! A simple and easy-to-manage guest book component with Captcha, in the component settings you can define prohibited words that will not be displayed, you can also protect yourself from the automatic publication of unwanted messages by additionally enabling message moderation.

  • To manage the component and messages, you need to log in through the site's administrative panel with your username and password.

  • On the control panel, find the “Components” tab, hover over it and select the component you are interested in from the drop-down list; in this case, we are interested in the guest book component “Phoca Guestbook” and since the material is about how to manage messages, click on the “messages” tab "

  • In the new “Messages” window that opens, you will see all the messages in the guest book. Unpublished messages are marked with a red circle.
    You can:
  1. publish an unpublished message by clicking on the red circle;
  2. publish an unpublished message by checking the box next to the message and clicking on the “Publish” button at the top;
  3. first read the unpublished message, edit it if necessary;
  4. create a new message yourself.

  • To read the guest book message, click on the subject of the message. The message editing page “Message: [edit]” will open. If the message is not spam, then you can select “Published” in the column on the right “Publishing Settings” and click on the “Save” or “Save and Close” button.

  • To create a new guestbook message, click on the “Create” button (orange cross). The message creation form “Message: [edit]” will open.
  • Fill out the fields: Subject and Username. Then be sure to select the name of the guest book in the “Guest Book” field, enter the text of the message, in the column on the right “Publishing Settings”, select “Published” and click on the “Save” or “Save and Close” button. After this, the message will appear on the website.

Publications on the topic