PHP Novice on the road (vi)

Source: Internet
Author: User
5.5 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)


{


/* 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


printf ("<CENTER><B>%06d</B></CENTER> <BR> n",


get_hitcount ("Counter.txt"));


include ("Include/footer.inc");


?>


, look at our new front.php3
.




5.6 Feedback Form





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.


<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.





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.





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 form: 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 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 ">


<input type= "text" Name= "Searchstr" 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, call grep query


echo "<hr>n";


//Call grep query for case insensitive mode of 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 the 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 the following can be processed and exported


if (count ($myresult))


{


echo "<ol>n";


while (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");


?>








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.








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.