<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Novices Design Courses &#187; guest book</title>
	<atom:link href="http://novices-design-courses.info/tag/guest-book/feed/" rel="self" type="application/rss+xml" />
	<link>http://novices-design-courses.info</link>
	<description>Design Blog</description>
	<lastBuildDate>Thu, 15 Oct 2009 05:09:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The guest book on PHP/MySQL</title>
		<link>http://novices-design-courses.info/the-guest-book-on-phpmysql/</link>
		<comments>http://novices-design-courses.info/the-guest-book-on-phpmysql/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 06:57:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[guest book]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://novices-design-courses.info/?p=56</guid>
		<description><![CDATA[Today I shall tell to you about how to write the guest book on PHP and MySQL. Anything complex{difficult} in it no, and opportunities given guest not the biggest: a paginal conclusion of recordings, check of the entered data, an opportunity to delete recording.
Today I shall tell to you about how to write the guest [...]]]></description>
			<content:encoded><![CDATA[<p>Today I shall tell to you about how to write the guest book on PHP and MySQL. Anything complex{difficult} in it no, and opportunities given guest not the biggest: a paginal conclusion of recordings, check of the entered data, an opportunity to delete recording.<span id="more-56"></span></p>
<p>Today I shall tell to you about how to write the guest book on PHP and MySQL. Anything complex{difficult} in it no, and opportunities given guest not the biggest: a paginal conclusion of recordings, check of the entered data, an opportunity to delete recording.</p>
<p>We admit{allow}, that at you already is PHP, MySQL and the web &#8211; server. All of you have established and have adjusted.</p>
<p>Let&#8217;s start with creation of the table in which the data of our guest book will be stored{kept}. We shall ask the user a name and the comment. At desire the user can inform e-mail addresses and a homepage. For administration of the book we need one more field, unique for each recording, &#8211; the identifier. Well and date, certainly. In a result such table turns out:</p>
<p>CREATE TABLE gb (</p>
<p>id int (10) unsigned NOT NULL auto_increment,</p>
<p>datetime datetime DEFAULT &#8216; 0000-00-00 00:00:00 &#8216; NOT NULL,</p>
<p>name varchar (100) NOT NULL,</p>
<p>email varchar (100),</p>
<p>www varchar (100),</p>
<p>message text NOT NULL,</p>
<p>PRIMARY KEY (id)</p>
<p>);</p>
<p>The table at us is. Now it is possible to start programming.</p>
<p>For we shall create a file with adjustments of the guest book:</p>
<p>&lt;? php</p>
<p>// The general{common} constants</p>
<p>define (&#8216;PATH&#8217;, &#8216;/gb / &#8216;); // a way to the guest book</p>
<p>define (&#8216; RECSPERPAGE &#8216;, 10); // quantity{amount} of recordings on one page</p>
<p>define (&#8216; ADMIN_EMAIL &#8216;,&#8217; artem@sapegin.ru &#8216;); // email the manager</p>
<p>define (&#8216; ERROR_LOG_FILE &#8216;,&#8217; logs/error.log &#8216;); // a file of a broad gully of mistakes</p>
<p>// Parameters of a DB</p>
<p>define (&#8216; DBHOST &#8216;,&#8217; localhost &#8216;); // the host name</p>
<p>define (&#8216; DBUSER &#8216;,&#8217; root &#8216;); // a login name</p>
<p>define (&#8216; DBPASSWD &#8216;, &#8220;); // the password</p>
<p>define (&#8216; DBNAME &#8216;,&#8217; test &#8216;); // a name of a database</p>
<p>?&gt;</p>
<p>Now we shall think, what auxiliary functions will be necessary for us. On it will be necessary to cooperate with SUBD, will check up and to process the data entered by the user. As for functions of administration on it is required to distinguish the manager from simple users.</p>
<p>Let&#8217;s start with job with SUBD.</p>
<p>&lt;? php</p>
<p>/ ** recource db_connect (string host, string user, string passwd, string dbname)</p>
<p>* Connection to SUBD and opening of a database</p>
<p>*/</p>
<p>function db_connect ($host, $user, $passwd, $dbname)</p>
<p>{</p>
<p>$link = mysql_pconnect ($host, $user, $passwd) or die (&#8216; Could not connect to database &#8216;);</p>
<p>mysql_select_db ($dbname) or die (&#8216; Could not select database &#8216;);</p>
<p>return $link;</p>
<p>}</p>
<p>/ ** Carries out search to a DB</p>
<p>*</p>
<p>* @param the text of search</p>
<p>* @return resource id</p>
<p>*/</p>
<p>function db_query ($query)</p>
<p>{</p>
<p>$result = mysql_query ($query)</p>
<p>or die (&#8216; Bad database query &#8216;);</p>
<p>return $result;</p>
<p>}</p>
<p>/ ** Carries out search to a DB (placeholder)</p>
<p>*</p>
<p>* @param the text of search</p>
<p>* @param*</p>
<p>* @return resource id</p>
<p>*/</p>
<p>function db_query_ex ($query)</p>
<p>{</p>
<p>$values = func_get_args ();</p>
<p>array_shift ($values);</p>
<p>$i = 0;</p>
<p>return db_query (preg_replace (&#8216; %? %e &#8216;,&#8217; &#8220;&#8216;&#8221; .addslashes ($values [$i ++]). &#8220;&#8216;&#8221; &#8216;,</p>
<p>$query));</p>
<p>}</p>
<p>?&gt;</p>
<p>Processing of lines (check and kill of the data entered by the user).</p>
<p>&lt;? php</p>
<p>/ **</p>
<p>* Whether checks is the line e-mail address</p>
<p>*/</p>
<p>function strings_isemail ($string)</p>
<p>{</p>
<p>return preg_match (&#8216; % [-.w] + [-w] + (?:. [-w] +) + % &#8216;, $string);</p>
<p>}</p>
<p>/ **</p>
<p>* Addition of links on http and e-mail</p>
<p>*/</p>
<p>function strings_addlinks ($string)</p>
<p>{</p>
<p>return preg_replace (</p>
<p>&#8216; % ((?:http|ftp): // [-w] + (?:. [-w] +) +b [-w: and? = +! / ~ * $. ' %] *) (? &lt;! [.?!)]) % i &#8216;,</p>
<p>&#8216; &lt;a href = &#8220;\1 &#8220;&gt; \1 &lt;a&gt; &#8216;,</p>
<p>$string</p>
<p>);</p>
<p>}</p>
<p>/ **</p>
<p>* Cleaning a line</p>
<p>*/</p>
<p>function strings_clear ($string)</p>
<p>{</p>
<p>$string = trim ($string);</p>
<p>$string = stripslashes ($string);</p>
<p>return htmlspecialchars ($string, ENT_QUOTES);</p>
<p>}</p>
<p>/ **</p>
<p>* Trimming a line</p>
<p>*/</p>
<p>function strings_stripstring ($text, $wrap, $length)</p>
<p>{</p>
<p>$text = preg_replace (&#8216; % (S {&#8216;. $ wrap. &#8216;}) % &#8216;, &#8216;\1, $text);</p>
<p>return substr ($text, 0, $length);</p>
<p>}</p>
<p>?&gt;</p>
<p>autentifikacii the manager I leave a spelling to you as the domestic task. There are many ways and their discussion &#8211; a subject of separate clause{article}. I shall result only function &#8211; zaglushku:</p>
<p>&lt;? php</p>
<p>/ **</p>
<p>* Check: the manager or the usual user</p>
<p>*/</p>
<p>function auth_is_admin ()</p>
<p>{</p>
<p>return $ _GET [' admin '];</p>
<p>}</p>
<p>?&gt;</p>
<p>Further goes enough the big module which contains almost all HTML-code of the guest book, &#8211; a pattern. In him there is nothing complex{difficult} and his{its} spelling is possible quite under force verstal`hhiku a site if at you those is present.</p>
<p>&lt;? php</p>
<p>/ **</p>
<p>* Heading of page</p>
<p>*/</p>
<p>function template_header ($page)</p>
<p>{</p>
<p>?&gt; &lt;html&gt;</p>
<p>&lt;head&gt;</p>
<p>&lt;title&gt; page &lt;? = $page?&gt; &lt;fjGuestbook Demo &lt;/title&gt;</p>
<p>&lt;style&gt;</p>
<p>body {</p>
<p>padding: 15px;</p>
<p>margin: 0;</p>
<p>color: *333;</p>
<p>background-color: *eee;</p>
<p>border-left: 30px solid *adba8e;</p>
<p>font: 500 .9em verdana, arial, helvetica;</p>
<p>}</p>
<p>a:link {color: *250;}</p>
<p>a:visited {color: *639;}</p>
<p>a:active, a:hover {</p>
<p>color: *c00;</p>
<p>text-decoration: underline;</p>
<p>}</p>
<p>h1 {font-size: 150 %;}</p>
<p>h2 {font-size: 110 %;}</p>
<p>.c {margin-bottom: 10px;}</p>
<p>.cn {</p>
<p>background-color: *d2d6bc;</p>
<p>padding: 2px 4px;</p>
<p>margin-bottom: 4px;</p>
<p>}</p>
<p>&lt;/style&gt;</p>
<p>&lt;/head&gt;</p>
<p>&lt;body&gt;</p>
<p>&lt;h1&gt; fjGuestbook Demo &lt;/h1&gt; &lt;? php</p>
<p>}</p>
<p>/ **</p>
<p>* The termination{ending} of page</p>
<p>*/</p>
<p>function template_footer ()</p>
<p>{</p>
<p>?&gt;</p>
<p>&lt;p&gt; fjGuestbook 1.2. Copyright © 2002-2004</p>
<p>&lt;a href = &#8221; http: // sapegin.ru &#8220;&gt; Artem Sapegin &lt;/a&gt; &lt;/p&gt;</p>
<p>&lt;/body&gt; &lt;/html&gt;</p>
<p>&lt;? php</p>
<p>}</p>
<p>/ **</p>
<p>* The form of addition of new recording</p>
<p>*/</p>
<p>function template_form ($name, $email, $www, $message, $error)</p>
<p>{</p>
<p>// A conclusion of the message on a mistake</p>
<p>function error ($error)</p>
<p>{</p>
<p>if ($error) echo &#8216; &lt;br&gt; &lt;font color = * 880000&gt; &#8216;. $ error.</p>
<p>&#8216; &lt;/font&gt; &#8216;;</p>
<p>}</p>
<p>echo &#8216; &lt;h2&gt; To add the new message &lt;/h2&gt;</p>
<p>&lt;p&gt; &lt;table cellspacing = &#8220;2&#8243; cellpadding = &#8220;2&#8243; border = &#8220;0&#8243;&gt;</p>
<p>&lt;form action = &#8216;.PATH. &#8216;? add=1 method=post&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; the Name &lt;font color = * 880000&gt; * &lt;/font&gt;: &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;input type=text name = &#8220;name&#8221; size=30</p>
<p>maxlength=100 value = &#8221; &#8216;. $ name. &#8216; &#8220;&gt; &#8216;;</p>
<p>@error ($error [' name ']);</p>
<p>echo &#8216; &lt;/td&gt;</p>
<p>&lt;/tr&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; Email: &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;input type=text name = &#8220;email&#8221; size=30</p>
<p>maxlength=100 value = &#8221; &#8216;. $ email. &#8216; &#8220;&gt; &#8216;;</p>
<p>@error ($error [' email ']);</p>
<p>echo &#8216; &lt;/td&gt;</p>
<p>&lt;/tr&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; URL: &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;input type=text name = &#8220;www&#8221; size=30</p>
<p>maxlength=100 value = &#8221; &#8216;. $ www. &#8216; &#8220;&gt; &#8216;;</p>
<p>echo &#8216; &lt;/td&gt;</p>
<p>&lt;/tr&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; the Message &lt;font color = * 880000&gt; * &lt;/font&gt;: &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;textarea cols=40 rows=5</p>
<p>name = &#8220;message&#8221;&gt; &#8216;. $ message. &#8216; &lt;/textarea&gt; &#8216;;</p>
<p>@error ($error [' message ']);</p>
<p>echo &#8216; &lt;/td&gt;</p>
<p>&lt;/tr&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;small&gt; &lt;font color = * 880000&gt; * &lt;/font&gt;</p>
<p>- Obligatory fields &lt;/small&gt; &lt;/td&gt;</p>
<p>&lt;/tr&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;input name = &#8220;sb&#8221; type=submit</p>
<p>value = &#8221; To add the message &#8220;&gt; &lt;/td&gt;</p>
<p>&lt;/form&gt; &lt;/tr&gt;</p>
<p>&lt;/table&gt; &#8216;;</p>
<p>}</p>
<p>/ **</p>
<p>* A seal of one recording the guest book</p>
<p>*/</p>
<p>function template_show_body ($id, $name, $email, $www, $message, $datetime)</p>
<p>{</p>
<p>$out = &#8216; &lt;div&gt; &lt;div&gt; &lt;b&gt; &#8216;. $ name. &#8216; &lt;/b&gt; &#8216;;</p>
<p>// If is email or homepage &#8211; it is printed them</p>
<p>if ($email || $www)</p>
<p>{</p>
<p>$out. = &#8216; (&#8216;;</p>
<p>if ($email)</p>
<p>$out. = &#8216; &lt;a href=mailto: &#8216;. $ email. &#8216;&gt; email &lt;/a&gt; &#8216;;</p>
<p>if ($email ** $www)</p>
<p>$out. = &#8216; | &#8216;;</p>
<p>if ($www)</p>
<p>$out. = &#8216; &lt;a href = &#8216;. $ www. &#8216;&gt; www &lt;/a&gt; &#8216;;</p>
<p>$out. = &#8216;) &#8216;;</p>
<p>}</p>
<p>$out. = &#8216; writes &#8216;. $ datetime. &#8216;: &lt;/div&gt; &#8216;. $ message. &#8216; &lt;/div&gt; &#8216;;</p>
<p>// If the guest book is looked through by the manager &#8211; print the button</p>
<p>// Removals{Distances} of recording</p>
<p>if (auth_is_admin ())</p>
<p>{</p>
<p>$out. = &#8216; &lt;div&gt; [&lt;a href = '.PATH. '? admin=1*del = '. $ id.</p>
<p>'&gt; to remove &lt;/a&gt;] &lt;/div&gt; &#8216;;</p>
<p>}</p>
<p>return $out;</p>
<p>}</p>
<p>?&gt;</p>
<p>And, we at last have reached the main thing. Up to the module of the guest book. I shall try to write more comments that to you it was understandable.</p>
<p>&lt;? php</p>
<p>/ **</p>
<p>* Creation of the table if she{it} still no</p>
<p>*/</p>
<p>function gb_install ()</p>
<p>{</p>
<p>db_query (</p>
<p>&#8216; CREATE TABLE IF NOT EXISTS gb (</p>
<p>id int (10) unsigned NOT NULL auto_increment,</p>
<p>datetime datetime NOT NULL default &#8216; 0000-00-00 00:00:00 &#8216;,</p>
<p>name varchar (100) NOT NULL default &#8220;,</p>
<p>email varchar (100) default NULL,</p>
<p>www varchar (100) default NULL,</p>
<p>message text NOT NULL,</p>
<p>PRIMARY KEY (id),</p>
<p>INDEX (datetime)</p>
<p>) TYPE=MyISAM; &#8216;</p>
<p>);</p>
<p>}</p>
<p>/ **</p>
<p>* Addition of recording in the guest book</p>
<p>*/</p>
<p>function gb_add ($name, $email, $www, $message, and $error)</p>
<p>{</p>
<p>// We check correctness of filling of fields</p>
<p>$error = &#8220;;</p>
<p>if (empty ($name))</p>
<p>$error [' name '] = &#8216; This obligatory field &#8216;;</p>
<p>if (empty ($message))</p>
<p>$error [' message '] = &#8216; This obligatory field &#8216;;</p>
<p>if (! empty ($email) **! strings_isemail ($email))</p>
<p>$error [' email '] = &#8216; It not email &#8216;;</p>
<p>// If not was mistakes &#8211; it is added</p>
<p>if (! $error)</p>
<p>{</p>
<p>// We clean the data</p>
<p>$name = strings_clear ($name);</p>
<p>$message = strings_clear ($message);</p>
<p>$name = strings_stripstring ($name, 15, 100);</p>
<p>$email = strings_stripstring ($email, 100, 100);</p>
<p>$www = strings_stripstring ($www, 100, 100);</p>
<p>$message = strings_stripstring ($message, 100, 2000);</p>
<p>$message = nl2br ($message);</p>
<p>// If the user was too lazy to write http:// before the address &#8211; we shall make</p>
<p>// It for him{it}</p>
<p>if (! empty ($www) ** &#8216; http:// &#8216;! = substr ($www, 0, 7))</p>
<p>$www = &#8216; http: // &#8216;. $ www;</p>
<p>// Search about addition of recording in a database</p>
<p>db_query_ex (&#8216; INSERT INTO gb (name, email, www, message, datetime)</p>
<p>VALUES (????, NOW ()) &#8216;, $name, $email, $www, $message);</p>
<p>// We throw a browser on the first page</p>
<p>// It is necessary, that if the user will press button Refresh,</p>
<p>// Recording was not added once again</p>
<p>header (&#8216; Location: &#8216;.PATH. &#8220;? page=1 &#8220;);</p>
<p>}</p>
<p>}</p>
<p>// Removal{Distance} of recording from the guest book</p>
<p>function gb_delete ($id)</p>
<p>{</p>
<p>// Search about removal{distance} of recording from a database</p>
<p>// WHERE id = &#8216;. $ id specifies recording which should be removed</p>
<p>db_query_ex (&#8216; DELETE FROM gb WHERE id =? &#8216;, $id);</p>
<p>header (&#8216; Location: &#8216;.PATH. &#8220;? page=1 &#8220;); //???</p>
<p>}</p>
<p>// A conclusion of page with recordings</p>
<p>function gb_show ($page)</p>
<p>{</p>
<p>// Position of the first recording page</p>
<p>$begin = ($page &#8211; 1) * 10;</p>
<p>// Sample of recordings of a database</p>
<p>// SELECT * FROM gb &#8211; all fields from bd gb</p>
<p>// ORDER BY datetime DESC &#8211; sorting by date of, new from above</p>
<p>// LIMIT &#8216;. $ begin. &#8216;, &#8216;.RECSPERPAGE &#8211; restriction:</p>
<p>// RECSPERPAGE (see defines.php) recordings since $begin</p>
<p>$result = db_query (&#8216; SELECT * FROM gb ORDER BY datetime DESC LIMIT &#8216;.</p>
<p>$begin. &#8216;, &#8216;.RECSPERPAGE);</p>
<p>$out = &#8220;;</p>
<p>// A cycle on all chosen recordings</p>
<p>while ($row = mysql_fetch_array ($result))</p>
<p>$out. = template_show_body ($row [' id '], $row [' name '], $row [' email '],</p>
<p>$row [' www '], $row [' message '], $row [' datetime ']);</p>
<p>// We destroy result</p>
<p>mysql_free_result ($result);</p>
<p>echo $out;</p>
<p>}</p>
<p>// A conclusion of the list of pages</p>
<p>function gb_showpages ($current)</p>
<p>{</p>
<p>// We learn{We find out} number of recordings in the guest book</p>
<p>$result = db_query (&#8216; SELECT * FROM gb &#8216;);</p>
<p>$rows = mysql_num_rows ($result);</p>
<p>if ($rows)</p>
<p>{</p>
<p>$pages = ceil ($rows / RECSPERPAGE);</p>
<p>// We print links to pages (number{room} of the current page is not the link)</p>
<p>echo &#8216; &lt;div&gt; &#8216;;</p>
<p>for ($i = 1; $i &lt;= $pages; $i ++)</p>
<p>{</p>
<p>if ($i! = $current)</p>
<p>echo &#8216; | &lt;a href = &#8216;.PATH. &#8216;? page = &#8216;. $ i. &#8216;&gt;&#8217;. $ i. &#8216; &lt;/a&gt; &#8216;;</p>
<p>else</p>
<p>echo &#8216; | &#8216;. $ i;</p>
<p>}</p>
<p>echo &#8216; | &#8216;;</p>
<p>// If it not polslednjaja page we print the link &#8220;Further&#8221;</p>
<p>if ($current &lt;$pages)</p>
<p>echo &#8216;&gt; a href = &#8216;.PATH. &#8216;? page = &#8216;. ($ current + 1).</p>
<p>&#8216;&gt; is Farther&gt;&gt; &lt;/a&gt; &#8216;;</p>
<p>echo &#8216; &lt;/div&gt; &#8216;;</p>
<p>}</p>
<p>}</p>
<p>?&gt;</p>
<p>And the last &#8211; it is united all together.</p>
<p>&lt;? php</p>
<p>/ **</p>
<p>* fjGuestbook 1.2</p>
<p>*</p>
<p>* A nucleus of the guest book</p>
<p>*</p>
<p>* Copyright 2002-2004 Artem Sapegin</p>
<p>* http://sapegin.ru</p>
<p>*/</p>
<p>// We connect modules</p>
<p>require_once &#8216; my/defines.php &#8216;;</p>
<p>require_once &#8216; my/template.php &#8216;;</p>
<p>require_once &#8216; engine/lib/strings.php &#8216;;</p>
<p>require_once &#8216; engine/lib/auth.php &#8216;;</p>
<p>require_once &#8216; engine/lib/bd.php &#8216;;</p>
<p>require_once &#8216; engine/gb.php &#8216;;</p>
<p>// We are connected to a DB</p>
<p>db_connect (DBHOST, DBUSER, DBPASSWD, DBNAME);</p>
<p>// We create the table if she{it} no</p>
<p>gb_install ();</p>
<p>// We obtain the given forms if the form has been sent</p>
<p>if (! empty ($ _POST [' sb ']))</p>
<p>{</p>
<p>$name = $ _POST [' name '];</p>
<p>$email = $ _POST [' email '];</p>
<p>$www = $ _POST [' www '];</p>
<p>$message = $ _POST [' message '];</p>
<p>$formerr = &#8220;;</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>$name = $email = $www = $message = $formerr = &#8220;;</p>
<p>}</p>
<p>// If number{room} of page is not specified in GET-search, we deduce{remove} the first</p>
<p>if (is_numeric ($ _GET [' page ']))</p>
<p>$page = $ _GET [' page '];</p>
<p>else</p>
<p>$page = 1;</p>
<p>// If it is necessary to add recording, we add</p>
<p>if ($ _GET [' add '])</p>
<p>gb_add ($name, $email, $www, $message, $formerr);</p>
<p>// If it is necessary to remove recording, we delete</p>
<p>if (isset ($ _GET [' del ']) ** auth_is_admin ())</p>
<p>gb_delete (intval ($ _GET [' del ']));</p>
<p>// We print the guest book</p>
<p>template_header ($page);</p>
<p>gb_showpages ($page);</p>
<p>gb_show ($page);</p>
<p>gb_showpages ($page);</p>
<p>template_form ($name, $email, $www, $message, $formerr);</p>
<p>template_footer ();</p>
<p>?&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://novices-design-courses.info/the-guest-book-on-phpmysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The guest book step by step</title>
		<link>http://novices-design-courses.info/the-guest-book-step-by-step/</link>
		<comments>http://novices-design-courses.info/the-guest-book-step-by-step/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 06:56:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[guest book]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://novices-design-courses.info/?p=54</guid>
		<description><![CDATA[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.
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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.<span id="more-54"></span></p>
<p>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.</p>
<p>So, for the beginning let&#8217;s be defined{determined}, that we shall write. In a result we should receive the guest book with the following properties:</p>
<p>* The visitor necessarily should enter the name and the message, and at will the e-mail and the address of a homepage.</p>
<p>* Splitting into pages.</p>
<p>* Our guest book should work with register_globals = Off.</p>
<p>* All recordings are stored{kept} in a usual text file (i.e. we will not need a database).</p>
<p>* The guest book will consist of one file.</p>
<p>The note. All below-mentioned have been tested on PHP 4.3.0.</p>
<p>With problems{tasks} like were defined{determined}, now let&#8217;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:</p>
<p>&lt;form action = &#8220;gb.php&#8221; method = &#8220;post&#8221;&gt;</p>
<p>&lt;table width = &#8220;500&#8243; cellpadding = &#8220;2&#8243;</p>
<p>cellspacing = &#8220;0&#8243; style = &#8221; border: 1px solid rgb (0, 75, 151); &#8221; bgcolor = &#8221; * d7ecff &#8220;&gt;</p>
<p>&lt;tbody&gt; &lt;tr&gt;</p>
<p>&lt;td align = &#8220;right&#8221;&gt;</p>
<p>* A name:</p>
<p>&lt;/td&gt;</p>
<p>&lt;td align = &#8220;left&#8221;&gt;</p>
<p>&lt;input type = &#8220;text&#8221; name = &#8220;msg_from&#8221; maxlength = &#8220;40&#8243; size = &#8220;30&#8243;&gt;</p>
<p>&lt;/td&gt;</p>
<p>&lt;/tr&gt;</p>
<p>&lt;tr&gt;</p>
<p>&lt;td align = &#8220;right&#8221;&gt;</p>
<p>E-Mail:</p>
<p>&lt;/td&gt;</p>
<p>&lt;td align = &#8220;left&#8221;&gt;</p>
<p>&lt;input type = &#8220;text&#8221; name = &#8220;msg_mail&#8221; maxlength = &#8220;40&#8243; size = &#8220;30&#8243;&gt;</p>
<p>&lt;/td&gt;</p>
<p>&lt;/tr&gt;</p>
<p>&lt;tr&gt;</p>
<p>&lt;td align = &#8220;right&#8221;&gt;</p>
<p>** URL:</p>
<p>&lt;/td&gt;</p>
<p>&lt;td align = &#8220;left&#8221;&gt;</p>
<p>&lt;input type = &#8220;text&#8221; name = &#8220;msg_url&#8221; maxlength = &#8220;40&#8243; size = &#8220;30&#8243;&gt;</p>
<p>&lt;/td&gt;</p>
<p>&lt;/tr&gt;</p>
<p>&lt;tr&gt;</p>
<p>&lt;td align = &#8220;right&#8221;&gt;</p>
<p>* The message:</p>
<p>&lt;/td&gt;</p>
<p>&lt;td align = &#8220;left&#8221;&gt;</p>
<p>&lt;textarea cols = &#8220;45&#8243; rows = &#8220;7&#8243; name = &#8220;msg_message&#8221;&gt; &lt;/textarea&gt;</p>
<p>&lt;/td&gt;</p>
<p>&lt;/tr&gt;</p>
<p>&lt;tr&gt;</p>
<p>&lt;td align = &#8220;center&#8221; colspan = &#8220;2&#8243;&gt;</p>
<p>&lt;input type = &#8220;submit&#8221; name = &#8220;msg_submit&#8221; value = &#8220;To add&#8221;&gt;</p>
<p>&lt;input type = &#8220;reset&#8221;&gt;</p>
<p>&lt;/td&gt;</p>
<p>&lt;/tr&gt;</p>
<p>&lt;tr&gt;</p>
<p>&lt;td align = &#8220;center&#8221; colspan = &#8220;2&#8243;&gt;</p>
<p>* Fields obligatory for filling &lt;br&gt;</p>
<p>** url to enter without http://</p>
<p>&lt;/td&gt;</p>
<p>&lt;/tr&gt;</p>
<p>&lt;/tbody&gt; &lt;/table&gt;</p>
<p>&lt;/form&gt;</p>
<p>Now we shall understand with the basic adjustments of our guest book:</p>
<p>//&#8212;&#8212;&#8212;-Adjustments GB&#8212;&#8212;&#8212;-//</p>
<p>$file_gb = &#8220;c:/HTTP/CoderPRO/site/gb.dat&#8221;; // a file where recordings GB are stored{kept}</p>
<p>$max_rec = 128; // a maximum quantity of recordings in a file</p>
<p>$rec_page = 6; // quantity{amount} of recordings deduced{removed} on one page</p>
<p>//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-//</p>
<p>Let&#8217;s write a code of heading of our page:</p>
<p>&lt;html&gt; &lt;head&gt;</p>
<p>&lt;title..&gt;:: the Guest book::.. &lt;/title&gt;</p>
<p>&lt;meta http-equiv = &#8220;Pragma&#8221; content = &#8220;no-cache&#8221;&gt;</p>
<p>&lt;meta http-equiv = &#8220;expires&#8221; content = &#8220;0&#8243;&gt;</p>
<p>&lt;/head&gt;</p>
<p>&lt;body&gt;</p>
<p>&lt;div align = &#8220;center&#8221;&gt; &lt;h1&gt; the Guest book &lt;/h1&gt; &lt;/div&gt;</p>
<p>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 &#8211; if a browser does not understand the first parameter Pragma, that a storage time of our page in a cache &#8211; 0 sek.</p>
<p>Now we shall start actually programming. For the beginning we shall write function of check of the entered data:</p>
<p>// Check of the entered data //</p>
<p>function test () {</p>
<p>global $HTTP_POST_VARS;</p>
<p>if (! isset ($HTTP_POST_VARS [' msg_from '], $HTTP_POST_VARS [' msg_mail '],</p>
<p>$HTTP_POST_VARS [' msg_url '], $HTTP_POST_VARS [' msg_message '])) {</p>
<p>echo &#8221; &lt;p align = &#8220;center&#8221;&gt; the Mistake at transfer of parameters to a script!</p>
<p>Address to the manager of a site. &lt;/p&gt; n &#8220;;</p>
<p>return (false);</p>
<p>}</p>
<p>if (trim ($HTTP_POST_VARS [' msg_from ']) == &#8221; &#8220;) {</p>
<p>echo &#8221; &lt;p align = &#8220;center&#8221;&gt; you have not entered the name. Repeat input. &lt;/p&gt; n &#8220;;</p>
<p>return (false);</p>
<p>}</p>
<p>if (trim ($HTTP_POST_VARS [' msg_message ']) == &#8221; &#8220;) {</p>
<p>echo &#8221; &lt;p align = &#8220;center&#8221;&gt; you have not entered the message. Repeat input. &lt;/p&gt; n &#8220;;</p>
<p>return (false);</p>
<p>}</p>
<p>return (true);</p>
<p>} // test ()</p>
<p>$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.</p>
<p>Let&#8217;s consider as at us will be it is stored{kept} recordings in a file. In one line of a file &#8211; one recording in the following format:</p>
<p>Imja|E-Mail|URL|Data And time dobavlenija|Soobhhenie</p>
<p>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 &#8211; 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 &lt;br&gt; 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:</p>
<p>// Function of addition of recording //</p>
<p>function add () {</p>
<p>global $max_rec;</p>
<p>global $file_gb;</p>
<p>global $HTTP_POST_VARS;</p>
<p>$recs = @file ($file_gb) or $recs = array ();</p>
<p>$from = $HTTP_POST_VARS [' msg_from '];</p>
<p>$mail = $HTTP_POST_VARS [' msg_mail '];</p>
<p>$url = $HTTP_POST_VARS [' msg_url '];</p>
<p>$message = $HTTP_POST_VARS [' msg_message '];</p>
<p>$from = str_replace (&#8220;|&#8221;, &#8221; *brvbar; &#8220;, $from);</p>
<p>$mail = str_replace (&#8220;|&#8221;, &#8221; *brvbar; &#8220;, $mail);</p>
<p>$url = str_replace (&#8220;|&#8221;, &#8221; *brvbar; &#8220;, $url);</p>
<p>if (strlen ($message)&gt; 1000) $message=substr ($message, 0,1000);</p>
<p>$message = htmlspecialchars ($message, ENT_QUOTES);</p>
<p>$message = str_replace (&#8220;|&#8221;, &#8221; *brvbar; &#8220;, $message);</p>
<p>$message = trim ($message);</p>
<p>$message = ereg_replace (&#8221;</p>
<p>&#8220;,&#8221; &lt;br&gt; &#8220;, $message);</p>
<p>array_unshift ($recs, &#8221; $from | $ mail | $ url | &#8220;.date (&#8221; d M Y, H:i &#8220;). &#8221; | $messagen &#8220;);</p>
<p>if (count ($recs)&gt; $max_rec) $recs=array_slice ($recs, 0, $max_rec);</p>
<p>$count = count ($recs);</p>
<p>$f = fopen ($file_gb, &#8220;w&#8221;);</p>
<p>for ($i=0; $i &lt;$count; $i ++) {</p>
<p>fwrite ($f, $recs [$i]);</p>
<p>}</p>
<p>fclose ($f);</p>
<p>} // add ()</p>
<p>$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 &#8211; 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.</p>
<p>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 &#8211; have deduced{removed} according to design and everything, but in fact we want to draw a conclusion paginal &#8211; 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}.</p>
<p>// Function of a conclusion of recordings //</p>
<p>function view () {</p>
<p>global $file_gb;</p>
<p>global $HTTP_GET_VARS;</p>
<p>global $rec_page;</p>
<p>if (file_exists ($file_gb)) {</p>
<p>$messages = file ($file_gb);</p>
<p>$count = count ($messages);</p>
<p>if ($count&gt; $rec_page) {</p>
<p>nav_page (ceil ($count / $ rec_page), (isset ($HTTP_GET_VARS [' page '])?</p>
<p>$HTTP_GET_VARS [' page ']: 1), &#8221; gb.php? page = &#8220;);</p>
<p>echo &#8220;&lt;br&gt;&#8221;;</p>
<p>}</p>
<p>$num_page=1;</p>
<p>if (isset ($HTTP_GET_VARS [' page '])) {</p>
<p>if (($HTTP_GET_VARS [' page ']&gt; 0) and</p>
<p>($HTTP_GET_VARS [' page '] &lt;=ceil ($count / $ rec_page)))</p>
<p>$num_page = $ HTTP_GET_VARS [' page '];</p>
<p>}</p>
<p>for ($i = ($ num_page-1) * $rec_page; $i &lt;= (($num_page * $ rec_page &lt;$count)?</p>
<p>$num_page * $ rec_page-1: $count-1); $i ++) {</p>
<p>$tmp = explode (&#8220;|&#8221;, $messages [$i]);</p>
<p>echo &#8221; &lt;table class = &#8220;text_info&#8221; border = &#8220;0&#8243; width = &#8221; 100 % &#8220;&gt; n &#8220;;</p>
<p>echo &#8221; &lt;tr class = &#8220;sel_p&#8221;&gt; n &#8220;;</p>
<p>echo &#8221; &lt;td&gt; n &#8220;;</p>
<p>echo &#8221; &lt;b&gt; From: &lt;/b&gt; &lt;br&gt; &#8220;;</p>
<p>if ($tmp [2] &lt;&gt; &#8221; &#8220;) echo &#8221; &lt;b&gt; URL: &lt;/b&gt; &lt;br&gt; &#8220;;</p>
<p>echo &#8221; &lt;b&gt; Date: &lt;/b&gt; n &#8220;;</p>
<p>echo &#8221; &lt;/td&gt; n &#8220;;</p>
<p>echo &#8221; &lt;td class = &#8220;text_info_sel&#8221; width = &#8221; 100 % &#8220;&gt; n &#8220;;</p>
<p>echo $tmp [0];</p>
<p>if ($tmp [1] &lt;&gt; &#8221; &#8220;) {echo &#8221; | &lt;a href = &#8221; mailto: &#8220;. $ tmp [1]. &#8221; &#8220;&gt; &#8220;. $tmp [1.] &#8221; &lt;/a&gt; &#8220;;}</p>
<p>echo &#8220;&lt;br&gt;&#8221;;</p>
<p>if ($tmp [2] &lt;&gt; &#8221; &#8220;) echo &#8221; &lt;a href = &#8221; http: // &#8220;. $tmp [2]. &#8221; &#8220;&gt; &#8220;. $tmp [2.] &#8221; &lt;/a&gt; &lt;br&gt; &#8220;;</p>
<p>echo $tmp [3.] &#8220;n&#8221;;</p>
<p>echo &#8221; &lt;/td&gt; n &lt;/tr&gt; n &#8220;;</p>
<p>echo &#8221; &lt;td colspan = &#8220;2&#8243;&gt; &lt;br&gt; n &#8220;;</p>
<p>echo $tmp [4];</p>
<p>echo &#8221; &lt;/td&gt; n &lt;/tr&gt; n &lt;/table&gt; n &#8220;;</p>
<p>echo &#8220;&lt;br&gt;&#8221;;</p>
<p>} // for</p>
<p>if ($count&gt; $rec_page) {nav_page (ceil ($count / $ rec_page),</p>
<p>(isset ($HTTP_GET_VARS [' page '])? $HTTP_GET_VARS [' page ']: 1), &#8221; gb.php? page = &#8220;);}</p>
<p>echo &#8220;&lt;br&gt;&#8221;;</p>
<p>} else {</p>
<p>echo &#8221; &lt;center&gt; Recordings no. You can be the first;) &lt;/center&gt; &lt;br&gt; n &#8220;;</p>
<p>}</p>
<p>} // view ()</p>
<p>Global variables: $file_gb &#8211; a file with recordings, $rec_page &#8211; deduced{removed} on one page &#8211; 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 &#8211; whether there is a file with recordings &#8211; 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&#8217;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.</p>
<p>// Function of a conclusion of navigation on pages //</p>
<p>function nav_page (</p>
<p>$count, // the General{Common} kol-in pages</p>
<p>$num_page, // Number{Room} of the current page</p>
<p>$url // What URL for the link to page</p>
<p>// (to it{him} number{room} of page) is added</p>
<p>) {</p>
<p>$page_nav = 3; // how much pages to deduce{remove} simultaneously</p>
<p>$begin_loop=1; // initial value in a cycle</p>
<p>$end_loop = $ count; // final value in a cycle</p>
<p>echo &#8221; &lt;div align = &#8220;center&#8221;&gt; [Pages ($count): ";</p>
<p>// Check on a correctness of number{room} of the current page</p>
<p>if ($num_page&gt; $count or $num_page &lt;1) $num_page=1;</p>
<p>// Further in function there is a conclusion of navigation, all is received here by practical consideration</p>
<p>if ($num_page&gt; $page_nav) {</p>
<p>echo " &lt;a href = " $ url ". ($page_nav * (floor ($num_page / $ page_nav)-</p>
<p>($num_page % $ page_nav == 0? 1 : 0))). " "&gt; (". ($page_nav * (floor ($num_page / $ page_nav)-</p>
<p>1-($ num_page % $ page_nav == 0? 1 : 0)) +1). "-". ($page_nav * (floor ($num_page / $ page_nav)-</p>
<p>($num_page % $ page_nav == 0? 1 : 0))). ") &lt;/a...&gt; ";</p>
<p>$begin_loop = $ page_nav * (floor ($num_page / $ page_nav) - ($num_page % $ page_nav == 0? 1 : 0)) +1;</p>
<p>}</p>
<p>if ($count&gt; $page_nav * (floor ($num_page / $ page_nav) - ($num_page % $ page_nav == 0? 1 : 0) +1)) {</p>
<p>$end_loop = $ page_nav*ceil ($num_page / $ page_nav);</p>
<p>}</p>
<p>for ($i = $begin_loop; $i &lt;= $end_loop; $i ++) {</p>
<p>if ($i == $num_page) echo " *nbsp; &lt;b&gt; $i &lt;/b&gt; ";</p>
<p>else echo " *nbsp; &lt;a href = " $ url$i "&gt; $i &lt;/a&gt; ";</p>
<p>} // for</p>
<p>if ($count&gt; $page_nav * (floor ($num_page / $ page_nav) - ($num_page % $ page_nav == 0? 1 : 0) +1)) {</p>
<p>echo " *nbsp; *nbsp;... &lt;a href = " $ url ". ($page_nav*ceil ($num_page / $ page_nav) + 1).</p>
<p>" "&gt; (". ($page_nav*ceil ($num_page / $ page_nav) + 1);</p>
<p>if ($page_nav*ceil ($num_page / $ page_nav) +1 &lt;$count) {</p>
<p>echo "-". ($count &lt;= $page_nav * (ceil ($num_page / $ page_nav) +1)?</p>
<p>$count: $page_nav * (ceil ($num_page / $ page_nav) +1));</p>
<p>}</p>
<p>echo ") &lt;/a&gt; ";</p>
<p>}</p>
<p>echo " *nbsp; *nbsp;] &lt;/div&gt; n &#8220;;</p>
<p>} // nav_page ()</p>
<p>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 &#8211; 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 &#8211; the Name, 1-st &#8211; e-mail, 2-nd &#8211; url, 3-rd &#8211; date and time of addition, 4-th &#8211; the message. Well and further we deduce{remove} our recording as we want.</p>
<p>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:</p>
<p>if (isset ($HTTP_POST_VARS [' msg_submit '])) {if (test ()) add ();}</p>
<p>view ();</p>
<p>I think anything complex{difficult} in him there are no also you will understand that where:)</p>
<p>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.</p>
<p>To whom laziness all to collect &#8211; can download available. See a file gb.zip.</p>
<p>Let&#8217;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 &#8211; I shall necessarily answer.</p>
]]></content:encoded>
			<wfw:commentRss>http://novices-design-courses.info/the-guest-book-step-by-step/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
