Start with the instance

Source: Internet
Author: User
Tags anonymous array empty header html page implement include split
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;
Read the contents of the counter file if it already exists
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>%06d</B></CENTER> <BR> \ n",
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.

It's easy to get form data in PHP. When a form is sent, each element contained in the form is assigned a value, which can be used as a reference to a generic variable.
<form name= "MyForm" action= "process_form.php3" method= "POST" >
<input type= "TEXT" name= "MyText" value= "Some VALUE" >
</FORM>

In process_form.php3, the variable $mytext is given the input value-very simple! Similarly, you can get variable values from list boxes, multiple selection boxes, radio boxes, buttons, and other form elements. The only thing you have to do is name each element of the form so that it can be referenced in the future.

Based on this method, we can generate a simple three-element form: Name, e-mail address, and message. When the viewer sends the form, the PHP page that processes the form (SENDFDBK.PHP3) reads the data, checks to see if the name is empty, and then mail you the data.

Form: form.php3
?
Include ("Include/common.inc");
$title = "Feedback";
Include ("Include/header.inc");
?>
<P>
<form action= "sendfdbk.php3" method= "POST" >
<input type= "text" name= "name" value= "Your name" size= "Maxlength=" >
<input type= "text" maxlength= "width=" value= "Your Email" name= "Email" >
<BR>
<textarea rows= "7" cols= "name=" "Comment" >
Your feedback on the My home page.
</TEXTAREA>
<BR>
<input type= "Submit" value= "Send feedback!" >
</FORM>
</P>
?
Include ("Include/footer.inc");
?>

Processing forms: SENDFDBK.PHP3
?
Include ("Include/common.inc");
$title = "Feedback";
Include ("Include/header.inc");
if ($name = = "")
{
Now I hate anonymous messages!
echo "Duh? How to come you are anonymous? ";
}
ElseIf ($name = = "Your name")
{
This visitor really does not want to disclose the name Ah!
echo "Hello?" <b>your name</b> is supposed to being replaced with
your actual name!</b> ";
}
Else
{
Output a polite thank you language
echo "
Hello, $name.
<BR>
Thank for your feedback. It is greatly appreciated.
<BR>
Thanking
<BR>
$MyName <BR>
$MyEmailLink
";
Finally Mail out
Mail ($MyEmail, "Feedback.", "
Name: $name
E-mail: $email
Comment: $comment
");
}
Include ("Include/footer.inc");
?>

3.5 Simple in-site search engine

PHP can invoke external programs. In the UNIX environment we can use the program grep to implement a simple search engine. We can do a little bit more complicated: Use a page to output a form for the user to input the search string and output the query results.
?
Include ("Include/common.inc");
$title = "Search";
Include ("Include/header.inc");
?>
<P>
<form action= ". echo "$PHP _self";?> "method=" POST >
<input type= "text" name= "Searchstr" value= " echo "$searchstr";?> "
Size= "Maxlength=" >
<input type= "Submit" value= "search!" >
</FORM>
</P>
?
if (! empty ($SEARCHSTR))
{
Empty () is used to check whether the query string is empty
If not empty, the grep query is invoked
echo "Invoke grep query for case insensitive mode for all files
$cmdstr = "Grep-i $searchstr *";
$fp = Popen ($cmdstr, "R"); Execute command and output pipeline
$myresult = Array (); Store query Results
while ($buffer = FGETSS ($fp, 4096))
{
grep returns this format: file name: Number of rows in a matching string
So we use function split () to separate processing data
List ($fname, $fline) = Split (":", $buffer, 2);
We only output the results of the first match
if (!defined ($myresult [$fname]))
$myresult [$fname] = $fline;
}
Now we store the results in an array, and then we can process and output the
if (count ($myresult))
{
echo "<ol>\n";
while (the list ($fname, $fline) = each ($myresult))
echo "<LI>
<a href=\ "$fname \" > $fname </A>: $fline </li>\n ";
echo "</ol>\n";
}
Else
{
If no query results
echo "Sorry. Search on <B> $searchstr </B>
Returned no results.<br>\n ";
}
Pclose ($FP);
}
?>
?
Include ("Include/footer.inc");
?>


Comments:

Php_self is a built-in variable in PHP. Contains the current file name.
Fgets () reads the file by row, up to 4096 (specified) character length.
FGETSS () is similar to fgets (), just the HTML tag that parses the output.
The split () has a parameter of 2, because we only need to divide the output into two parts. In addition, you need to omit the ":".
Each () is an array action function that is used to more easily traverse the entire array.
The functions of Popen (), Pclose () and fopen (), fclose () are very similar, except for the addition of pipeline processing.
Please note that the above code is not a good way to implement a search engine. This is just one example that will help us learn more about PHP. Ideally, you should create a database that contains keywords and then search.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.