Many features of PHP are related to other software or tools. With the PHP knowledge we have learned so far, we can try to build a simple interactive website. Using this process, we can learn a lot. Ok, we are now focusing on the construction of a typical personal website.
3.1 Plan a site generally one personal site includes a welcome page, a message page, a bookmark link page, a counter, contact information, or even photos and some music files, etc. Let's start with a title page, a contact information page and a resume page. We also need standard general page heads and bottom.
Title page --front.html
Here we have a very simple HTML file:
Welcome to my cold house, although there is nothing happened now. P>
But I hope that I can get more immediately. P>
copyright? I myself, 1999 i> small> p> body> html>
Contact Information Page - Count.html
Similarly, we have a simple page:
You can contact me by 1-800-PHP-INFO p>
copyright? I myself 1999 i> small> p> body> html>
3.2 HTML to PHP
As you can see above, each page has the same head and bottom. As mentioned above, you can write the same information when the workload is less, but you want to have more than 100 pages and you need to change your head or at the bottom? What is a lengthy thing of the handmade change on one page! So we should write PHP head and bottom files for these pages, then we will quote them in each HTML page. We will put these include files under a sub-directory called include. Below we will write the general content of these sites into the file.
General Variable Setting: Common.inc // Total Universal Variable $ MyEmail = "PHPTalk@tnc.org"; $ myemaillink = " $ myemail A> "; $ myName =" php talk "; $ mysitename = $ myName." 's home point ";?>
General Page Head: Header.inc // Define Universal Page Head?>
Copyright? By Echo "$ myname ($ myemaillink)";>, 1999 i> small> < / P> body> html>
New page front.php3: Include ("incrude / common.inc"); $ title = "welcome"; include ("incrude / header.inc");?>
Welcome to my cold house, though There is no current time for the time being. P>
But I hope that I can get more immediately. P> Include ("incrude / footer.inc");?>
New Cont.php3: Include ("incrude / common.inc"); $ title = "contact information"; include ("include / header.inc");?>
You can pass 1-800- PHP-INFO Contact me p> Include ("incrude / footer.inc");?>
Now you can guess the benefits of this arrangement. If you want to change the head or bottom of the page, you only need to change the corresponding file. If you want to modify your e-mail address or even your name, just modify the common.inc file. Also worth noting is that you can include files with any file name or file extension into your file, you can even contain files on other sites.
3.3 counter
Let's add a counter on the homepage. This example has been speaking many times, but it is still conducive to how to read and write files and create their own functions. Counter.inc contains the following code: / * || A simple counter * / function get_hitcount ($ counter_file) {/ * zero the counter This If the counter is not used, the initial value will be 1 you can of course also The initial value is set to 20000 to deceive * / $ count = 0; // If the storage counter file already exists, read the content IF (File_exists ($ counter ($ counter_file, "r" ); // We only take the top 20, I hope your site should not be too popular. $ Count = 0 fgets ($ fp, 20); // Due to the function fgets () returns a string, we can use 0 The method automatically converts it to an integer fclose ($ fp); // Take the file operation} // Add a count value $ count ; // Write the new count value to file $ fp = fopen ($ counter_file, "w "); FPUTS ($ fp, $ count); fclose ($ fp); # 数 数 值 RETURN ($ count);}?> and then we change the Front.php3 file to display this counter: incrude (" including include / Counter.inc "); // I put the count value in the file counter.txt, read and output Printf ("
Let's add a feedback form so that your viewer fills in and e-mail. For example, we implement it in a very simple way, we only need two pages: a input form for the viewer; a getting form data and processes, mail gives you.
Getting form data in PHP is very simple. When a form is sent, the individual elements contained in the form is assigned a corresponding value, so that it can be used as the general variable is referenced.