The guest book on PHP
The guest book - one of useful tools of the Web-master, allowing to learn{find out} opinion on the job, that is on the created site. Now we shall consider as to write the guest book own hands (only driver " prjamye_ruki.dll " establish:) also have gone...)
Let's pass to algorithm of our guest book. The user opens the guest book and sees a maximum quantity of messages which is set by you. On same page there is a form for input of messages. And in the bottom of page we shall make pages that the user could look through predydushie messages.
After the user has entered the data, the text of the message and has pressed button " Send ", a script zapisyvyet this information in the beginning of a file that other users could see messages " from the last ".
In a file of the guest book we shall write down the following information:
? A login name
? Date and time (the manager can establish itself, on the taste) sendings of the message
? The message
? E-mail the user (at desire of the user)
? ICQ (at desire of the user)
? The house. Page (at desire of the user)
? IP the user (only for the manager)
Total: 7 fields. After each field costs{stands} spec. A symbol ¹01
As at addition of the message all fields will be will be checked up on tegi to increase bezopastnost` our guest book.
Listing the form for addition of messages is below resulted, save it on a disk as a file with the name "form.txt":
<script language = "JavaScript" type = "text/javascript">
function checkForm () {
formErrors = false;
if (document.post.mes.value.length <2) {
formErrors = " you should enter the text of the message ";
} else
if (document.post.mes.value.length> 1000) {
var simbols = document.post.mes.value.length;
formErrors = " you should enter the text of the message of less than 1000 symbols, and at you " + simbols;
} else
if (document.post.user.value.length> 20) {
var simbols = document.post.user.value.length;
formErrors = " you should enter your name of less than 20 symbols, and at you " + simbols;
} else
if (document.post.user.value.length <3) {
var simbols = document.post.user.value.length;
formErrors = " you should enter your name of more than 3 symbols ";
}
if (formErrors) {
alert (formErrors);
return false;
} else {
bbstyle (-1);
// formObj.preview.disabled = true;
// formObj.submit.disabled = true;
return true;
}
}
</script>
<form action = " index.php? action=addmes " method = "post" name = "post" onsubmit = " return checkForm (this) " enctype = "multipart/form-data">
<table cellspacing = "0">
<tr> <td height = "5" style = " border-top:1px solid *000000; " colspan = "2"> <font size = "1"> *nbsp; </font> </td> </tr>
<tr>
<td width = "250">
<b> <font size = "2" color = " * 003333 "> <font color = "DD0000"> * </font> your name: </font> </b>
</td>
<td width = "298">
<font size = "1" face = "Verdana">
<input name = "user" value = "" style = " width:298; " maxlength = "20"> </font> <b> <font size = "2"> </input>
</font>
</b>
</td>
<td width = "2"> </td>
</tr>
<tr>
<td width = "250">
<b> <font size = "2" color = " * 003333 "> EHlekstronnaja mail: </font> </b>
</td>
<td width = "298">
<font size = "1" face = "Verdana">
<input name = "mail" value = "" style = " width:298; " maxlength = "20"> </font> <b> <font size = "2"> </input>
</font>
</b>
</td>
<td width = "2"> </td>
</tr>
<tr>
<td width = "250">
<b> <font size = "2" color = " * 003333 "> the House. Page: </font> </b>
</td>
<td width = "298">
<font size = "1" face = "Verdana">
<input name = "homepage" value = " http: // " style = " width:298; " maxlength = "30"> </font> <b> <font size = "2"> </input>
</font>
</b>
</td>
<td width = "2"> </td>
</tr>
<tr>
<td width = "250">
<b> <font size = "2" color = " * 003333 "> ICQ: </font> </b>
</td>
<td width = "298">
<font size = "1" face = "Verdana">
<input name = "icq" value = "" style = " width:298; " maxlength = "12"> </font> <b> <font size = "2"> </input>
</font>
</b>
</td>
<td width = "2"> </td>
</tr>
<tr>
<td width = "550" colspan = "3">
<b>
<font size = "2" color = " * 003333 "> <font color = "DD0000"> * </font> the Text of the message: (there are more 1000 symbols) <br>
</font> </b> <font size = "1" face = "Verdana">
<textarea name = "mes" style = " width:550; " rows = "6"> </textarea> </font> <b> <font size = "2"> <br>
</font>
</b> <img alt = "" src = "image/zero.gif" height = "2" width = "1"> <br>
<button type = "submit"> <font face = "Verdana"> To send </font> </button>
<img alt = "" src = "image/zero.gif" height = "1" width = "2">
<button type = "reset"> <font face = "Verdana"> To erase </font> </button>
</td>
<td width = "2"> </td>
</tr>
</table>
</form>
Listing a file for a conclusion of messages is below resulted, save it on a disk as a file with the name "tamples.txt":
<table cellpadding = "0" cellspacing = "0" width = " 100 % ">
<tr>
<td bgcolor = " * E4E4E4 " colspan = "2" height = "20"> <font style = " font-size: 10pt; ">
*nbsp; *nbsp; <a href = " mailto: <? echo $email;?> "> <b> <? echo $nick;?> </b> </a> - <a href = " <? echo $homepage;?> "> the house. Page </a> *nbsp; | icq: <? echo $icq;?> *nbsp; | Date: <? echo $date;?> </font>
</td>
</tr>
<tr> <td width = "5"> <img alt = "" src = "zero.gif" width = "5" height = "0"> </td> <td width = " 100 % "> <font size = "-1 "> <? echo $text;?> </font> </td> </tr>
</table> <br>
Listing a file for the answer to the added message is below resulted, save it on a disk as a file with the name "otvet.txt":
<html>
<head>
<title> Wait... </title>
<meta http-equiv = "refresh" content = " 2; url=index.php ">
<style type = "text/css">
html {
overflow-x: auto;
}
body {
background: *FFF;
color: *222;
font-family: Verdana, Tahoma, Arial, Trebuchet MS, Sans-Serif, Georgia, Courier, Times New Roman, Serif;
font-size: 11px;
line-height: 135 %;
margin: 0;
padding: 0;
text-align: center;
}
</style>
</head>
<body>
<div>
<br> <br> <br> <br> <br> <br>
<p> Thanks... <br>
For your message <br>
To us important each opinion </p>
<p> (<a href = "index.php"> Or press here if not want to wait </a>) </p>
</div>
</body>
</html>
Listing is below resulted, save it on a disk as a file with the name "index.php":
<? php
Error_Reporting (E_ALL and ~E_NOTICE);
**************** The information for the manager ****************
$file_guest = "guest.txt"; // a file of the guest book
$kol_fields = 7; // quantity{amount} of fields for each message
// (nick, date, text, email, icq, homepage, IP)
$max_mes_on_page = 20; // a maximum quantity of messages on page
$tamples_file = "tamples.txt"; // a file for a conclusion of messages
$addform = "form.txt"; // the form for addition of messages
$date_format = " d.m. Y / H:i "; // a format of date
$otvet = "otvet.txt"; // a file for the answer to addition of the message
**************** =============================== ****************
function strtosafe ($str) // Protection of the data that of the non-authorized influence
{
$str = nl2br (htmlspecialchars ($str)); // it is replaced spec. Symbols on their equivalents
$str = str_replace ("*", " and *035; ", $str);
$str = str_replace ('. ', " and *046; ", $str);
$slashes = chr (92);
$str = str_replace ($slashes, " and *092; ", $str);
$slashes = chr (39);
$str = str_replace ($slashes, " and *39; ", $str);
return $str;
}
function file_write ($COUNT_FILE, $text) // Rewrites a file
{
$f = @fopen ($COUNT_FILE, " w + "); // we open a file
if (@chmod ("$COUNT_FILE", 0777) == false)
@chmod ("$COUNT_FILE", 0777); // it is exposed rights for a file
@fwrite ($f, $text); // it is written down the data in a file
@fclose ($f); // we close a file
return 0;
}
function file_read ($COUNT_FILE) // Reading from a file ($file_name)
{
clearstatcache (); // it is cleared kesh a file
if (is_file ($COUNT_FILE))
{
$f = @fopen ($COUNT_FILE, "r");
if (@chmod ("$COUNT_FILE", 0777) == false)
@chmod ("$COUNT_FILE", 0777);
$conts = @fread ($f, @filesize ($COUNT_FILE));
@fclose ($f);
return $conts;
} else return 0;
}
function fewrite ($file_name, $text) // It Is done{made} dozapis` the data in a file ($file_name)
{
if (@chmod ("$file_name", 0777) == false)
@chmod ("$file_name", 0777);
$f = @fopen ($file_name, " a + ");
@flock ($f, Lock_EX);
@fwrite ($f, $text);
@flock ($f, LOCK_UN);
@fclose ($f);
return 0;
}
function fswrite ($file_name, $text) // It Is done{made} dozapis` the data in the beginning of a file ($file_name)
{
$file_temp = $file_name. '.tmp '; // the Reserve file
if (is_file ($file_temp)) die (" it was not possible to write down the information: last time job
Script it has been interrupted (failure in system). <br> Pozhajlusta address to the manager ");
if (@chmod ("$file_name", 0777) == false)
@chmod ("$file_name", 0777);
if (copy ($file_name, $file_temp))
{
file_write ($file_name, $text);
fewrite ($file_name, file_read ($file_temp));
@unlink ($file_temp);
} else return-1; // an error code (it was not possible to make copying)
return 0;
}
$action = $HTTP_GET_VARS ["action"]; // action
if ($action == ' addmes')
{
$nick = strtosafe ($ _POST [' user ']);
$date = date ($date_format);
$text = strtosafe ($ _POST [' mes']);
$email = strtosafe ($ _POST [' mail ']);
$icq = strtosafe ($ _POST [' icq ']);
$homepage = strtosafe ($ _POST [' homepage ']);
$IP = $ _SERVER [' REMOTE_ADDR '];
if (! is_file ($file_guest)) file_write ($file_guest, " "); // if file guest gnigi not suhhestvet then we create empty
fswrite ($file_guest, $user. "\x01." $date. "\x01." $text. "\x01." $email. "\x01." $icq. "\x01." $homepage. "\x01." $IP. "\x01"); // we write down the information
include ($otvet); // we insert the answer for the user upon termination of addition
} else {
$page = $HTTP_GET_VARS ["page"] - 1; // number{room} of page
if ($page <1) $page = 0;
$cont = file_read ($file_guest); // the data from a file
$conts = explode ("\x01", $cont); // it is written down the data from fala in a file
$all_mes = (count ($conts)-1) / 7; // total of messages
include ($addform);
for ($i = ($max_mes_on_page * $page); $i <(($max_mes_on_page * $page) + $max_mes_on_page); $i ++)
{
if ($i> = $all_mes) break;
$nick = $conts [$kol_fields * $ i];
$date = $conts [$kol_fields * $ i + 1];
$text = $conts [$kol_fields * $ i + 2];
$email = $conts [$kol_fields * $ i + 3];
$icq = $conts [$kol_fields * $ i + 4];
$homepage = $conts [$kol_fields * $ i + 5];
$ip = $conts [$kol_fields * $ i + 6];
include ($tamples_file); // preparation for a conclusion of messages
}
echo " Pages: ";
for ($i = 1; $i <($all_mes / $max_mes_on_page + 1); $i ++) // a conclusion of pages
echo " <a href = \ " index.php? page = ". $i. "\"> *nbsp; ". $i. " *nbsp; </a> ";
}
?>
I shall not begin to explain a code since has already signed comments in him.
This code is certainly imperfect, therefore nenado strongly to criticize. I specially have left unfinished some functions or nesdelal in general (that you would move the grey substance and have forced it to think:)). For example there was without attention that here there is no limit on show of pages, i.e. it is necessary to deduce{remove} on 10 pages and two strelochki: on previous 10 (if those are available) and on the following. As it is possible to add kh-ciju on check e-mail, icq and dom.stranicu. It is possible also BB-codes prisobachit` see more in detail sdes`. About administration I already and negovorju, do{make} as want
It is a little about protection of the data:
Create in deriktorii with a file "index.php" a file ".htaccess", and vpihhite in him the following information:
<Files "*.txt">
deny from all
</Files>
DirectoryIndex index.php
It pokhvolit to you to hide all *.txt files and to appoint the main file in deriktorii "index.php"
Like all has said, very well... Yes needs{requirements} at all different, this script will approach someone, and to someone and no. I have given idea to you, the rest do{make}. As they say: " On all you will not please ".

|