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)
{
/* To zero the counter
so if the counter has not been used, the initial value will be 1
you can, of course, set the initial value to 20000来 a liar
*/
$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 automatically convert it to an integer by adding a 0 method
fclose ($FP);
//Complete the operation of the file
}
//Add one time count
$count + +;
//Writes the new count value to the 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
Let's add a feedback form so that your viewers can fill it out and e-mail it 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.
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.
according to this method, we can generate a simple form containing three elements: 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.
//This viewer really do 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 you
<BR>
$MyName <BR>
$MyEmailLink
";
//finally mail out
Mail ($MyEmail, "Feedback.", "
Name: $name
e-mail: $email
Comment: $comment
");
}
include ("Include/footer.inc");
?>
Note: If the program works at the end of your test, check that your PHP profile (PHP3 is PHP3.INI,PHP4 php.in) is not set up properly. Because this program requires your PHP configuration file to be set as follows:
First, open your Php3.ini or php.ini file in Notepad to see if [Mail function] is set up, the default is as follows:
SMTP = localhost
Sendmail_from = me@localhost.com
SMTP server to SMTP, preferably your local SMTP server, I am here to 21CN SMTP server as an example, and then, in Sendmail_from Place your e-mail address, such as can be changed to:
SMTP = smtp.21cn.com
Sendmail_from = pert@21cn.com
changes do not forget to restart Apache,iis or PWS service OH.
5.7 Simple 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 ">
echo "Sorry. Search on <B> $searchstr </B>
returned no results.<br>n ";
}
Pclose ($FP);
}
?>
;?
include ("Include/footer.inc");
?>
Note:
php_self is a built-in variable in PHP. Contains the current file name.
fgets () reads files by row, up to 4096 (specified) character lengths.
The
fgetss () is similar to fgets (), just the HTML tag that parses the output.
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 manipulation function that is used to more easily traverse the entire array.
Popen (), Pclose () and fopen (), fclose () function very similar, but only increased 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.
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.