Let us add a counter to the homepage. This example has been told many times, but it is still 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) { /* Reset the counter to zero In this way, if the counter has not been used, the initial value will be 1 Of course you can also set the initial value to 20000 to trick people */ $count =0; // If the file storing the counter already exists, read its contents if ( file_exists($counter_file) ) { $fp=fopen($counter_file,"r") ; // We only took the top 20, I hope your site will not be too popular $count=0+fgets($fp,20); // Since the function fgets() returns String, we can automatically convert it to an integer by adding 0 fclose($fp); // File operation completed } // Increase the count value once $count++; //Write the new count value to 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 counter value in the file counter.txt, read it out and output it printf ("
< B>%06d
n", get_hitcount("counter.txt")); include("include/footer.inc"); ?> Check out our new front.php3
5.6 Feedback Form
Let us add another feedback form for your visitors to fill out and e-mail to you. For example, we use a very simple method to implement it. We only need two pages: one to provide the viewer with an input form; the other to obtain the form data, process it, and mail it to you.
Obtaining form data in PHP is very simple. When a form is sent, each element contained in the form is assigned a corresponding value, and can be used like a reference to a general variable.
In process_form.php3, the variable $mytext is assigned the entered value - very simple! Similarly, you can get variable values from form elements such as list boxes, multi-select boxes, radio boxes, buttons, etc. The only thing you have to do is give each element in the form a name so that you can reference it later.
Based on this method, we can generate a simple form containing three elements: name, e-mail address and message. When the visitor sends the form, the PHP page (sendfdbk.php3) that processes the form reads the data, checks whether the name is empty, and finally emails the data to you.
include("include/common.inc"); $title = "Feedback"; include("include/header.inc"); if ( $name == "" ) { // 现在我很讨厌匿名的留言! echo "Duh ? How come you are anonymous?"; } elseif ($name == "Your name") { // 这个浏览者真是不想透露姓名啊! echo "Hello ? Your name is supposed to be replaced with your actual name!"; } else { // 输出一段礼貌的感谢语 echo " Hello, $name.
Thank you for your feedback. It is greatly appreciated.
if ( ! empty($searchstr) ) { // empty( ) is used to check whether the query string is empty // If it is not empty, call grep query echo "n"; // Call grep to make all files case-insensitive Pattern query $cmdstr = "grep -i $searchstr *"; $fp = popen( $cmdstr, "r" ); // Execute command and output pipe $myresult = array() ; // Store query results while( $buffer = fgetss ($fp, 4096)) { // grep returns this format: File name: Number of lines in the matching string // Therefore, we use the function split() to separate and process the data list($fname, $fline) = split(":",$buffer, 2); // We only output the result of the first match if ( !defined($myresult[$fname])) $myresult[$fname] = $fline; } // Now we store the result in the array, and we can process it below Output if ( count($myresult) ) { echo "n"; while(list($fname,$fline) = each($myresult)) echo "
n"; echo "n "; } else { // If there are no search results echo "Sorry. Search on $searchstr 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 the file line by line, with a maximum length of 4096 (specified) characters. fgetss() is similar to fgets(), except that it parses the output HTML tags. split() has a parameter of 2, because we only need to split the output into two parts. Also need to omit ":". each() is an array operation function, used to traverse the entire array more conveniently. The functions of popen() and pclose() are very similar to fopen() and fclose(), except that pipeline processing is added. Please note that the above code is not a good way to implement a search engine. This is just an example to help us learn PHP better. Ideally you should build a database of keywords and then search them.
http://www.bkjia.com/PHPjc/317014.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317014.htmlTechArticleBuilding a simple interactive website (2) 5.5 Counter Let us add a counter to the homepage. This example has been told many times, but it is still useful to demonstrate how to read and write files to...
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn