PHP novice
This post is reprinted from: http://blog.9cbs.net/whzhaha/archive/2004/07/30/php.aspx
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, a 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:
p>
My Personal Homepage - Welcome P>
My personal page
welcome
Welcome to my cold house, although there is nothing to do now now.
But I hope that I can immediately get more.
Copyright? I myself, 1999
Contact Information Page - Count.html
Similarly we have a simple page:
My Personal Homepage - Contact Information
My personal page
Contact information
You can contact me through 1-800-PHP-INFO
Copyright? I myself, 1999
3.2 HTML to PHP
As you can see above, each page has the same head and bottom. As each page is as follows above, you can do it when you work 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
// General variable
$ Myemail = "phptalk@tnc.org";
$ Myemaillink = "$ myemail";
$ MyName = "php talk";
$ Mysitename = $ myName. "'S home page";
?>
General Page Head: Header.inc
/ / Define Universal Page Head
?>
p>
echo "$ mysitename - $ title";?> p>
General page: footer.inc
// General page
?>
Copyright? By
1999
New page Front.php3:
INCLUDE ("include / common.inc");
$ title = "Welcome";
INCLUDE ("include / header.inc");
?>
Welcome to my cold house, although there is nothing to do now now.
But I hope that I can immediately get more.
New Cont.php3:
INCLUDE ("include / common.inc");
$ title = "contact information";
INCLUDE ("include / header.inc");
?>
You can contact me through 1-800-PHP-INFO
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 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, it is worth noting 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)
{
/ * Retracence the counter
This will be 1 if the counter has not been used yet.
You can of course also set the initial value to 20000 to deceive people.
* /
$ count = 0;
// If the storage counter file already exists, read the content
IF (file_exists ($ counter_file))
{
$ fp = fopen ($ Counter_File, "R");
// We only took 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 automatically convert it to an integer by adding 0.
Fclose ($ fp);
// After the file is completed
}
/ / Add a count value
$ count ;
/ / Write new count value into the file
$ fp = fopen ($ Counter_File, "W");
FPUTS ($ FP, $ COUNT);
Fclose ($ fp);
# Return the count value
Return ($ count);
}
?>
Then we change the front.php3 file to display this counter:
INCLUDE ("include / counter.inc");
/ / I put the count value in the file counter.txt, read and output
Printf ("
% 06D
? / n ",
Get_hitcount ("counter.txt"));
Include ("include / footer.inc";
?>
Take a look at our new Front.php3
3.4 Feedback form
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.
In Process_Form.php3, the variable $ mytext is given the input value - very simple! Similarly, you can get a variable value from a list box, multi-select box, a radio box, buttons. The only thing you have to do is to name each element in the form for future reference.
According to this method, we can generate a form that contains three elements: name, e-mail address, and message. When the browser sends a form, process the PHP page of the form (SendFDBK.php3) read the data, check if the name is empty, and finally give you the data mail.
Form: form.php3
INCLUDE ("include / common.inc");
$ title = "feedback";
INCLUDE ("include / header.inc");
?>
INCLUDE ("include / footer.inc");?>
Processing form: sendfdbk.php3
INCLUDE ("include / common.inc");
$ title = "feedback";
INCLUDE ("include / header.inc");
IF ($ Name == "")
{
// I hate anonymous message!
Echo "duh? how come you are anonymous?";
}
Elseif ($ Name == "Your Name")
{
// This browser is really not named!
echo "Hello? Your name is supposed to be replaced? with
Your Actual Name! ";
}
Else
{
// Output a courteous thanks
echo "
Hello, $ Name.
Thank you for your feedback. It is getly appreciated.
Thanking you
$ MyName
$ Myemaillink
"
// Finally Mail out
Mail ($ MyEmail, "Feedback.", "
Name: $ name
E-mail: $ EMAIL
Comment: $ comment
");
}
Include ("include / footer.inc";
?>
3.5 Simple Search Engine
PHP can call external programs. In a UNIX environment we can use the program GREP to implement a simple search engine. We can do a slightly complex: Use a page to output a form for users to enter search strings and output query results.
INCLUDE ("include / common.inc");
$ title = "search";
INCLUDE ("include / header.inc");
?>
IF (! EMPTY ($ searchstr))
{
// EMPTY () is used to check if the query string is empty
/ / If it is not empty, call the GREP query
echo "
/ N ";
// Call GREP to check for non-sensitive mode for all files
$ cmdstr = "grep -i $ searchstr *";
$ fp = POPEN ($ cmdstr, "r"); // execute the command and output the pipeline
$ myResult = array (); // store query results
While ($ buffer = fgetss ($ fp, 4096)))
{
// Grep returns this format: File name: Match string appears
/ / So we use the 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, below, and output it.
IF (count ($ myResult))
{
echo "
/ N ";
While (List ($ FNAME, $ FLINE) = Each ($ MyResult))
echo "
$ FNAME: $ FLINE / N ";
echo "
/ N ";
}
Else
{
// If there is no query result
Echo "Sorry. Search on $ SearchStr
Returned no results./N ";
}
PClose ($ fp);
}
?>
Include ("include / footer.inc";
?>
Note:
PHP_SELF is a variable built in PHP. Contains the current file name.
FGETS () Read the file by line, up to 4096 (specified) character length.
FGETSS () is similar to fgets (), just analyzing the output HTML tag.
Split () has a parameter 2 because we only need to divide the output into two parts. In addition, it needs to be omitted ":".
EACH () is an array operation function that is more convenient to traverse the entire array.
POPEN (), PClose () is similar to fopen (), and fclose () is similar, just increase the pipe processing.
Please note that the above code is not a good way to implement a search engine. This is just an example that helps us better learn PHP. Ideally, you should establish a database containing the keyword and search.