Section 6 building a simple interactive website (II)

Source: Internet
Author: User
Build a simple interaction website (2) 5.5 Counter. let's add a counter on the homepage. This example has been mentioned many times, but it is helpful to demonstrate how to read and write files and create your own functions. Counter. inc contains the following code :? /* | A simple counter */functionget_hitcount ($ counter_file ){/*

Build a simple interaction website(II)

5.5Counter

Let's add a counter to the homepage. This exampleAlready mentionedMultiple times, but it is helpful to demonstrate how to read and write files and create your own functions.Counter. incContains the following code:

/*
|A simple counter
*/
FunctionGet_hitcount($Counter_file)
{
/*Returns the counter to zero.
In this case, if the counter is not used, the initial value is1
You can also set the initial value20000Come and lie
*/
$ Count = 0;
//If the stored counter file already exists, read the content.
If (File_exists($Counter_file))
{
$Fp=Fopen($Counter_file, "r");
//We only took the previous20Bit. I hope your site will not be too popular.
$ Count = 0 + fgets ($ fp, 20 );
//Because the functionFgets()Returns a string. we can add0Is automatically converted to an integer.
Fclose($Fp);
//File operation completed
}
//Increase the count value once
$ Count ++;
//Write new count value to file
$Fp=Fopen($Counter_file, "w");
Fputs($Fp, $ count);
Fclose($Fp);
#Return count value
Return ($ count );
}
?>

Then we changeFront. php3File to display this counter:
Include ("include/Counter. inc");
//I put the count value in the fileCounter.txtRead and output
Printf("

% 06d
N ",
Get_hitcount("Counter.txt "));
Include ("include/ Footer. inc");
?>
Look at our new Front. php3

5.6Feedback Form

Let's add another feedback form so that your browser can fill in andEmailHere you are. For example, we use a very simple method to implement it. we only need two pages: one to provide an input form for the viewer, and the other to obtain the table.Single Data processing,MailHere you are.

  PHPIt is very easy to obtain form data. When a form is sent, each element in the formAssignedThe corresponding value, which can be used as a reference to a common variable.

InProcess_form.php3Medium, variable$MytextThe input value is assigned.--Very simple! Similarly, you can obtain variable values from form elements such as the list box, multiple-choice box, single-choice, and Button. The only thing you have to do is name every element in the form so that it can be referenced in the future.

Based on this method, we can generate a simple form containing three elements: name,EmailAddress and message. After the viewer sends a formPHPPage (Sendfdbk. php3) Read the data, check whether the name is empty, and finally set the dataMailHere you are.

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 = "")
{
//Now I hate anonymous messages!
Echo "Duh? How come you are anonymous? ";
}
Elseif($ Name = "Your name ")
{
//This viewer really does not want to be named!
Echo "Hello?Your nameIs supposed to be replaced
Your actual name!";
}
Else
{
//Output a polite thank-you note
Echo"
Hello, $ name.


Thank you for your feedback. It is greatly appreciated.


Thanking you


$MyName

$MyEmailLink
";
//LastMailGo out
Mail ($MyEmail, "Feedback .","
Name: $ name
Email: $ email
Comment: $ comment
");
}
Include ("include/Footer. inc");
?>

Note: If this program is used during your testingLast energyWork properly. Please check yourPHPConfiguration file(PHP3IsPhp3.ini, PHP4IsPhp. in)Are there any settings. Because this program requires yourPHPConfigure the configuration file as follows:

First, useNotePadOpen yourPhp3.iniOrPhp. iniFile.[Mail function]Are there any settings? the default conditions are as follows:
SMTP =Localhost
Sendmail_from= Me@localhost.com
ToSMTPSetSMTPServer, preferably your localSMTPServer. here I use21cnOfSMTPServer as an exampleSendmail_fromEnter yourE-MAILAddress, for example:
SMTP = smtp.21cn.com
Sendmail_from= Pert@21cn.com
Do not forget to restart after modificationApache, IISOrPWSService.


5.7Simple intra-site search engine

  PHPAttackers can call external programs. InUnixIn the environment, we can use the programGrepImplement a simple search engine. We can make it a little more complicated: using 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 () Used to check whether the query string is null
//If not empty, callGrepQuery
Echo "n ";
//CallGrepQuery all files in case-insensitive mode
$Reverse Str="Grep-I$Searchstr*";
$Fp=Popen($Reverse Str, "R ");//Execute commands and output pipelines
$Myresult= Array ();//Store query results
While ($ buffer =Fgetss($Fp, 4096 ))
{
//GrepThe format is as follows: File name: number of lines in the matched string
//Therefore, we use the functionSplit ()Separate and process data
List ($Fname, $Fline) = Split (":", $ buffer, 2 );
//We only output the first matching result.
If (! Defined ($Myresult[$Fname])
$Myresult[$Fname] = $Fline;
}
//Now we store the results in an array, and we can process and output the results below.
If (count ($Myresult))
{
Echo"
    N ";
    While (list ($ Fname, $ fline) = Each ($ Myresult))
    Echo"

  1. Fname"> $Fname: $Fline
  2. N ";
    Echo"
N ";
}
Else
{
//If no query results exist
Echo "Sorry. Search on $Searchstr
Returned no results. <BR> n ";
}
Pclose($Fp);
}
?>
Include ("Include/Footer. inc");
?>


Note:

PHP_SELFYesPHPBuilt-in variables. Contains the current file name.
Fgets()Reads files by row, Max.4096(Specified) character length.
Fgetss()AndFgets()Similar, only parse the outputHTMLMark.
Split ()One parameter is2Because we only need to divide the output into two parts. In addition, this parameter must be omitted.":".
Each ()Is an array operation function, used to more easily traverse the entire array.
Popen(),Pclose()AndFopen(),Fclose()The functions are similar, but pipeline processing is added.
Note that the above code is not a good way to implement a search engine. This only helps us to learn better.PHPThis is just an example. Ideally, you should create a database containing keywords and search for them.

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.