Home
Home Page
Web Bases with LWP
Transformation relative in absoljutye links
For the greater information read the full documentation on LWP:: UserAgent.
Accessing HTTPS URLs
Job with the text and graphic data in common in PHP and MySQL
Change of appearance of the counter in CNStats
Simple banner system phpFBS
How to protect a site from total uploading.
21 mistake of programmer PHP
API functions
Minuses of use API of functions
Generation of the image
The guest book step by step
The guest book on PHP/MySQL
PHP - Simple caching
Even about protection e-mail addresses on webs - pages
Language of web - statistics
Program extract of the bill in system WebMoney
Language of web - statistics
Links

 

The guest book step by step

In this clause{article} we shall learn to create the guest book on PHP. And though this clause{article} is written for newbies, but you should possess even elements of programming on PHP.


So, for the beginning let's be defined{determined}, that we shall write. In a result we should receive the guest book with the following properties:


* The visitor necessarily should enter the name and the message, and at will the e-mail and the address of a homepage.

* Splitting into pages.

* Our guest book should work with register_globals = Off.

* All recordings are stored{kept} in a usual text file (i.e. we will not need a database).

* The guest book will consist of one file.


The note. All below-mentioned have been tested on PHP 4.3.0.


With problems{tasks} like were defined{determined}, now let's understand as at us all will be vygdjadet`. The form and recordings will be is deduced on one page. Our file will be called gb.php. We shall write a code of the form. At me it has turned out that:



<form action = "gb.php" method = "post">

<table width = "500" cellpadding = "2"

cellspacing = "0" style = " border: 1px solid rgb (0, 75, 151); " bgcolor = " * d7ecff ">

<tbody> <tr>

<td align = "right">

* A name:

</td>

<td align = "left">

<input type = "text" name = "msg_from" maxlength = "40" size = "30">

</td>

</tr>

<tr>

<td align = "right">

E-Mail:

</td>

<td align = "left">

<input type = "text" name = "msg_mail" maxlength = "40" size = "30">

</td>

</tr>

<tr>

<td align = "right">

** URL:

</td>

<td align = "left">

<input type = "text" name = "msg_url" maxlength = "40" size = "30">

</td>

</tr>

<tr>

<td align = "right">

* The message:

</td>

<td align = "left">

<textarea cols = "45" rows = "7" name = "msg_message"> </textarea>

</td>

</tr>

<tr>

<td align = "center" colspan = "2">

<input type = "submit" name = "msg_submit" value = "To add">

<input type = "reset">

</td>

</tr>

<tr>

<td align = "center" colspan = "2">

* Fields obligatory for filling <br>

** url to enter without http://

</td>

</tr>

</tbody> </table>

</form>


Now we shall understand with the basic adjustments of our guest book:



//----------Adjustments GB----------//

$file_gb = "c:/HTTP/CoderPRO/site/gb.dat"; // a file where recordings GB are stored{kept}

$max_rec = 128; // a maximum quantity of recordings in a file

$rec_page = 6; // quantity{amount} of recordings deduced{removed} on one page

//----------------------------------//


Let's write a code of heading of our page:



<html> <head>

<title..>:: the Guest book::.. </title>

<meta http-equiv = "Pragma" content = "no-cache">

<meta http-equiv = "expires" content = "0">

</head>

<body>

<div align = "center"> <h1> the Guest book </h1> </div>


Lines with META are necessary that our page not kehshirovalas`. The first line speaks a browser, that page kehshirovat` it is not necessary, and the second - if a browser does not understand the first parameter Pragma, that a storage time of our page in a cache - 0 sek.


Now we shall start actually programming. For the beginning we shall write function of check of the entered data:



// Check of the entered data //

function test () {

global $HTTP_POST_VARS;


if (! isset ($HTTP_POST_VARS [' msg_from '], $HTTP_POST_VARS [' msg_mail '],

$HTTP_POST_VARS [' msg_url '], $HTTP_POST_VARS [' msg_message '])) {

echo " <p align = "center"> the Mistake at transfer of parameters to a script!

Address to the manager of a site. </p> n ";

return (false);

}

if (trim ($HTTP_POST_VARS [' msg_from ']) == " ") {

echo " <p align = "center"> you have not entered the name. Repeat input. </p> n ";

return (false);

}

if (trim ($HTTP_POST_VARS [' msg_message ']) == " ") {

echo " <p align = "center"> you have not entered the message. Repeat input. </p> n ";

return (false);

}

return (true);

} // test ()


$HTTP_POST_VARS is a file containing all keys and values transferred{handed} by method POST (works at register_globals = Off). Whether all over again in function all is checked was transferred{handed}, and then whether obligatory fields the Name and the Message have been filled. Certainly, it is possible to make still check on a correctness entered e-mail and url and much that else, but it already if would be desirable you, do{make}, and we shall not be on it zamorachivat`sja and we shall go further.


Let's consider as at us will be it is stored{kept} recordings in a file. In one line of a file - one recording in the following format:


Imja|E-Mail|URL|Data And time dobavlenija|Soobhhenie


Before addition of recording we should a little podredaktirovat` the data. As it is possible to notice, for division of parts of recording we use a sign |, means our data should not contain this sign - we shall replace it{him} with analogue in a html-code: *brvbar;. Further we should check up length of the message, and if she more than some size (for example more than 1000 symbols) to cut off the message till the necessary length. Then we convert all special symbols in mnemonic HTML (function htmlspecialchars). And at last, we shall replace all carries of lines to the message on <br> and we shall format the data in the format necessary to us for recording in a file. Certainly here again it is possible to make still a heap of everyones pribambasov, but it we shall leave for independent jobs and we shall do{make} our guestbook further. So for recording in a file we have generated a line and now she{it} should be added in a file, thus not privysiv a limit of quantity{amount} of recordings in a file (recollect adjustments of our page: $max_rec = 128;) . I write something much, and a code it is not visible: to you our function of addition of recording in a file:



// Function of addition of recording //

function add () {

global $max_rec;

global $file_gb;

global $HTTP_POST_VARS;


$recs = @file ($file_gb) or $recs = array ();


$from = $HTTP_POST_VARS [' msg_from '];

$mail = $HTTP_POST_VARS [' msg_mail '];

$url = $HTTP_POST_VARS [' msg_url '];

$message = $HTTP_POST_VARS [' msg_message '];


$from = str_replace ("|", " *brvbar; ", $from);

$mail = str_replace ("|", " *brvbar; ", $mail);

$url = str_replace ("|", " *brvbar; ", $url);

if (strlen ($message)> 1000) $message=substr ($message, 0,1000);

$message = htmlspecialchars ($message, ENT_QUOTES);

$message = str_replace ("|", " *brvbar; ", $message);

$message = trim ($message);

$message = ereg_replace ("

"," <br> ", $message);


array_unshift ($recs, " $from | $ mail | $ url | ".date (" d M Y, H:i "). " | $messagen ");

if (count ($recs)> $max_rec) $recs=array_slice ($recs, 0, $max_rec);

$count = count ($recs);

$f = fopen ($file_gb, "w");

for ($i=0; $i <$count; $i ++) {

fwrite ($f, $recs [$i]);

}

fclose ($f);

} // add ()


$recs is a file with recordings. If the file $file_gb exists, with the help of function file () we appropriate{give} each line of a file to one element of a file, in an opposite case $recs - an empty file. Further there is an editing the data described above. Then with the help of function array_unshift () we add in the beginning of recordings our recording. Check on quantity{amount} of recordings further follows, and if quantity{amount} more necessary, with the help of function array_slice () we select the first elements in the quantity{amount} necessary to us. Well and, at last, we write down our recordings in a file.


So, for addition of recordings we have made functions, now it is necessary to make function for a paginal conclusion of the data to the screen. Basically in a conclusion of the data anything complex{difficult} no, counted recording - have deduced{removed} according to design and everything, but in fact we want to draw a conclusion paginal - in it just and there can be a complexity. So we shall start: First I shall result a code of function view (), and then we shall disassemble it{him}.



// Function of a conclusion of recordings //

function view () {

global $file_gb;

global $HTTP_GET_VARS;

global $rec_page;


if (file_exists ($file_gb)) {

$messages = file ($file_gb);

$count = count ($messages);

if ($count> $rec_page) {

nav_page (ceil ($count / $ rec_page), (isset ($HTTP_GET_VARS [' page '])?

$HTTP_GET_VARS [' page ']: 1), " gb.php? page = ");

echo "<br>";

}

$num_page=1;

if (isset ($HTTP_GET_VARS [' page '])) {

if (($HTTP_GET_VARS [' page ']> 0) and

($HTTP_GET_VARS [' page '] <=ceil ($count / $ rec_page)))

$num_page = $ HTTP_GET_VARS [' page '];

}

for ($i = ($ num_page-1) * $rec_page; $i <= (($num_page * $ rec_page <$count)?

$num_page * $ rec_page-1: $count-1); $i ++) {

$tmp = explode ("|", $messages [$i]);

echo " <table class = "text_info" border = "0" width = " 100 % "> n ";

echo " <tr class = "sel_p"> n ";

echo " <td> n ";

echo " <b> From: </b> <br> ";

if ($tmp [2] <> " ") echo " <b> URL: </b> <br> ";

echo " <b> Date: </b> n ";

echo " </td> n ";

echo " <td class = "text_info_sel" width = " 100 % "> n ";

echo $tmp [0];

if ($tmp [1] <> " ") {echo " | <a href = " mailto: ". $ tmp [1]. " "> ". $tmp [1.] " </a> ";}

echo "<br>";

if ($tmp [2] <> " ") echo " <a href = " http: // ". $tmp [2]. " "> ". $tmp [2.] " </a> <br> ";

echo $tmp [3.] "n";

echo " </td> n </tr> n ";

echo " <td colspan = "2"> <br> n ";

echo $tmp [4];

echo " </td> n </tr> n </table> n ";

echo "<br>";

} // for

if ($count> $rec_page) {nav_page (ceil ($count / $ rec_page),

(isset ($HTTP_GET_VARS [' page '])? $HTTP_GET_VARS [' page ']: 1), " gb.php? page = ");}

echo "<br>";

} else {

echo " <center> Recordings no. You can be the first;) </center> <br> n ";

}

} // view ()


Global variables: $file_gb - a file with recordings, $rec_page - deduced{removed} on one page - these variables we have defined{determined} quantity{amount} of recordings right at the beginning in adjustments of our guest book. And $HTTP_GET_VARS is a file containing all keys and values transferred{handed} by method GET, i.e. through URL (works at register_globals = Off) is to us it is required, since we shall just pass number{room} of page through URL. We shall understand, that our function does{makes}. First check is carried out - whether there is a file with recordings - and if is job on a conclusion of recordings and if there is no file the message is deduced is carried out, that recordings no. So, if it's OK and we have file with recordings with the help of function file () we create a file of lines of a file $messages, and a variable $count appropriate{give} kol-in elements of the received file, i.e. quantity{amount} of recordings. Further goes the conclusion of navigation of pages and recordings is carried out according to the necessary page. In particular we use function nav_page, I shall result only its{her} code.



// Function of a conclusion of navigation on pages //

function nav_page (

$count, // the General{Common} kol-in pages

$num_page, // Number{Room} of the current page

$url // What URL for the link to page

// (to it{him} number{room} of page) is added

) {


$page_nav = 3; // how much pages to deduce{remove} simultaneously


$begin_loop=1; // initial value in a cycle

$end_loop = $ count; // final value in a cycle

echo " <div align = "center"> [Pages ($count): ";

// Check on a correctness of number{room} of the current page

if ($num_page> $count or $num_page <1) $num_page=1;


// Further in function there is a conclusion of navigation, all is received here by practical consideration

if ($num_page> $page_nav) {

echo " <a href = " $ url ". ($page_nav * (floor ($num_page / $ page_nav)-

($num_page % $ page_nav == 0? 1 : 0))). " "> (". ($page_nav * (floor ($num_page / $ page_nav)-

1-($ num_page % $ page_nav == 0? 1 : 0)) +1). "-". ($page_nav * (floor ($num_page / $ page_nav)-

($num_page % $ page_nav == 0? 1 : 0))). ") </a...> ";

$begin_loop = $ page_nav * (floor ($num_page / $ page_nav) - ($num_page % $ page_nav == 0? 1 : 0)) +1;

}

if ($count> $page_nav * (floor ($num_page / $ page_nav) - ($num_page % $ page_nav == 0? 1 : 0) +1)) {

$end_loop = $ page_nav*ceil ($num_page / $ page_nav);

}

for ($i = $begin_loop; $i <= $end_loop; $i ++) {

if ($i == $num_page) echo " *nbsp; <b> $i </b> ";

else echo " *nbsp; <a href = " $ url$i "> $i </a> ";

} // for

if ($count> $page_nav * (floor ($num_page / $ page_nav) - ($num_page % $ page_nav == 0? 1 : 0) +1)) {

echo " *nbsp; *nbsp;... <a href = " $ url ". ($page_nav*ceil ($num_page / $ page_nav) + 1).

" "> (". ($page_nav*ceil ($num_page / $ page_nav) + 1);

if ($page_nav*ceil ($num_page / $ page_nav) +1 <$count) {

echo "-". ($count <= $page_nav * (ceil ($num_page / $ page_nav) +1)?

$count: $page_nav * (ceil ($num_page / $ page_nav) +1));

}

echo ") </a> ";

}

echo " *nbsp; *nbsp;] </div> n ";

} // nav_page ()


I shall not describe this piece, you can read about it in my clause{article} a paginal conclusion where all this is in detail described. Here I shall describe only how we deduce{remove} recordings. We have a cycle in which initial and final value $i - changes depending on page which we want to display. In a cycle we from an element of a file $messages [$i] create a file $tmp with the help of function explode which breaks a line into lines on borders formed{educated} by a separator |. I.e. we receive a file in which 0-th element - the Name, 1-st - e-mail, 2-nd - url, 3-rd - date and time of addition, 4-th - the message. Well and further we deduce{remove} our recording as we want.


We also have written all necessary functions for the guest book, remained only will write a code which to be carried out in the beginning, i.e. will choose, what function to start. He:



if (isset ($HTTP_POST_VARS [' msg_submit '])) {if (test ()) add ();}

view ();


I think anything complex{difficult} in him there are no also you will understand that where:)


All! We have written everything, that is necessary! Now collect all in a heap and receive the high-grade guest book. To collect in the following order: 1) Heading of a file, 2) Adjustments GB, 3) Functions, 4) our last written line, 5) the Form for addition, 6) a page header.


To whom laziness all to collect - can download available. See a file gb.zip.


Let's sum up: we have written the high-grade guest book and all problems{tasks} which have put before itself have carried out. Certainly it is possible to make still a heap of any chesspieces, to write administration of the guest book and many other things, but it already a subject of other clause{article}. In general, I hope clause{article} to you it was useful. If something was not understandable, favour I ask: write on a soap - I shall necessarily answer.