3 PHP Practice
Many of the features of PHP are related to other software or tools. Using the knowledge we've learned so far, we can try to build a simple interactive web site. We can learn a lot by using this process. Well, we're starting to focus on the construction of a typical personal website.
3.1 Planning a site
Generally a personal site includes a welcome page, a message This page, a bookmark link page, a counter, contact information, and even photo album and some music files and so on. Let's start with a title page, a contact information page, and a resume page. We also need standard generic page headers and bottoms.
Title Page--front.html
Here we have a very simple HTML file:
<HTML>
<HEAD>
<TITLE>
My personal homepage--Welcome
</TITLE>
</HEAD>
<BODY>
<H1>
My personal homepage
</H1>
<H2>
Welcome
</H2>
<HR>
<P>
Welcome to my humble abode, though there is nothing here for the time being.
</P>
<P>
But I hope to get more out of it right away.
</P>
<HR>
<p align= "CENTER" >
<SMALL> <I>
The Copyright? I myself, 1999
</I> </SMALL>
</P>
</BODY>
</HTML>
Contact Information page--count.html
Again, we have a simple page:
<HTML>
<HEAD>
<TITLE>
My personal Homepage--Contact information
</TITLE>
</HEAD>
<BODY>
<H1>
My personal homepage
</H1>
<H2>
Contact information
</H2>
<HR>
<P>
You can contact me through 1-800-php-info.
</P>
<HR>
<p align= "CENTER" >
<SMALL> <I>
The Copyright? I myself, 1999
</I> </SMALL>
</P>
</BODY>
</HTML>
3.2 HTML to PHP
From the above you can see that each page has the same head and bottom. Like the above, every page writes the same information, but it's OK to do it at less work, but imagine how much effort you would take to have more than 100 pages and you need to completely change the head or bottom? How tedious it is to make a page-by-page manual change! So we should write PHP headers and bottoms for these pages, and then we'll just have to reference them in each HTML page. We'll put these include files under a subdirectory called include. Below we will write the common content of these sites into the file.
Total Station general variable setting: Common.inc
?
Total Station General Variable
$myemail = "phptalk@tnc.org";
$myemaillink = "<a href=" Mailto:$myemail ">$MyEmail</a>";
$myname = "PHP Talk";
$mysitename = $myname. "' s home Page ";
?>
General Page head: Header.inc
?
Define common page headers
?>
<HTML>
<HEAD>
<TITLE>
? echo "$mysitename-$title";?>
</TITLE>
</HEAD>
<BODY>
<H1>
? echo "$mysitename";?>
</H1>
<H2>
? echo "$title";?>
</H2>
<HR>
Bottom of universal page: Footer.inc
?
Bottom of Universal page
?>
<HR>
<p align= "CENTER" >
<SMALL> <I>
The Copyright? By
? echo "$myname ($myemaillink)";?>
, 1999
</I> </SMALL>
</P>
</BODY>
</HTML>
New Page FRONT.PHP3:
?
Include ("Include/common.inc");
$title = "Welcome";
Include ("Include/header.inc");
?>
<P>
Welcome to my humble abode, though there is nothing here for the time being.
</P>
<P>
But I hope to get more out of it right away.
</P>
?
Include ("Include/footer.inc");
?>
The new cont.php3:
?
Include ("Include/common.inc");
$title = "Contact information";
Include ("Include/header.inc");
?>
<P>
You can contact me through 1-800-php-info.
</P>
?
Include ("Include/footer.inc");
?>
Now you can guess the benefits of this arrangement. If you want to change the head or bottom of the page, you just need to change the corresponding file. If you want to change your e-mail address or even your name, just modify the Common.inc file. It's also worth noting that you can include files with any file name or file extension in your file, and you can even include files from other sites.
3.3 Counter
Let's add a counter to the home page. This example has been said several times, but it is also useful to demonstrate how to read and write files and create your own functions. Counter.inc contains the following code:
?
/*
|| A simple counter
*/
function Get_hitcount ($counter_file)
{
/* return counter to zero
So if the counter has not been used, the initial value will be 1
You can set the initial value to 20000来, of course.
*/
$count=0;
If the counter file already exists, read the contents
if (file_exists ($counter_file))
{
$fp=fopen ($counter_file, "R");
We only take the top 20, I hope your site is not too popular AH
$count=0 fgets ($fp,20);
Because the function fgets () returns a string, we can convert it to an integer by adding a 0 method
Fclose ($FP);
Complete the operation of the file
}
Add a single count of values
$count;
Writes a new count value to a file
$fp=fopen ($counter_file, "w");
Fputs ($fp,$count);
Fclose ($FP);
# returns the Count value
return ($count);
}
?>
Then we change the Front.php3 file to display this counter:
?
Include ("Include/counter.inc");
I put the value in the file Counter.txt, read out and output
printf ("<CENTER><B>d</B></CENTER> <BR>",
Get_hitcount ("Counter.txt"));
Include ("Include/footer.inc");
?>
Look at our new front.php3.
3.4 Feedback Form
Let's add a feedback form for your visitors to fill out and e-mail to you. For example, we implement it in a very simple way, and we only need two pages: one for the viewer to provide input form, one to get the form data and processing, mail to you.