PHP Novice on the road (vi) _php Foundation

Source: Internet
Author: User
Tags anonymous php3 file
Building a simple interactive website (ii)

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)
{
/* return counter to zero
So if the counter has not been used, the initial value will be 1
You can set the initial value to 20000来, of course.
*/
$count = 0;
Read the contents of the counter file if it already exists
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 convert it to an integer by adding a 0 method
Fclose ($FP);
Complete the operation of the file
}
Add a single count of values
$count + +;
Writes a new count value to a 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 for your visitors to fill out and e-mail 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.

Based on this method, we can generate a simple three-element form: 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 forms: 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 visitor really does 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
<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 for 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 the [Mail function] is set up, and the default is as follows:
SMTP = localhost
Sendmail_from = me@localhost.com
Set SMTP server for SMTP, preferably your local SMTP server, where I take the 21CN SMTP server as an example, and then fill in your e-mail address at the Sendmail_from, for example, you can change to this:
SMTP = smtp.21cn.com
Sendmail_from = pert@21cn.com
Do not forget to restart Apache,iis or PWS service after the modification.


5.7 Simple in-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" value= " 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, the grep query is invoked
echo "Invoke grep query for case insensitive mode for 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 a 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 then we can process and output the
if (count ($myresult))
{
echo "<ol>n";
while (the 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");
?>


Comments:

Php_self is a built-in variable in PHP. Contains the current file name.
Fgets () reads the file by row, up to 4096 (specified) character length.
FGETSS () is similar to fgets (), just the HTML tag that parses the output.
The 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 action function that is used to more easily traverse the entire array.
The functions of Popen (), Pclose () and fopen (), fclose () are very similar, except for the addition of 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.