New driver on the road to new PHP road (vi)

Source: Internet
Author: User
Tags learn php php3 file
Building a simple interactive website (ii)
5.5 Counter
Let's add a counter to the home page. This example has been spoken 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
*/
function Get_hitcount ($counter _file)
{
/* Reset counter to zero
So if the counter is not yet in use, the initial value will be 1.
Of course you can set the initial value to 20000来.
*/
$count = 0;
If the store counter file already exists, read the contents
if (file_exists ($counter _file))
{
$FP =fopen ($counter _file, "R");
We only took the top 20, I hope your site is not too popular AH
$count =0+fgets ($FP, 20);
Since the function fgets () returns a string, we can automatically convert it to an integer by adding a 0 method
Fclose ($FP);
Operation completed on file
}
Increase the value of a single count
$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 show this counter:
Include ("Include/counter.inc");
I put the count value in the file Counter.txt, read out and output
printf (" %06d
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, we only need two pages: one provides the input form for the browser, one obtains the form data and processes, mail to you.
Getting the form data in PHP is simple. When a form is sent, the elements contained in the form are assigned the corresponding values, 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 form elements such as list boxes, multi-box, radio boxes, buttons, and so on. The only thing you have to do is to name each element of the form so that it can be referenced in the future.
Based on this method, we can generate a simple form that contains three elements: Name, e-mail address, and message. When the browser sends the form, the PHP page (sendfdbk.php3) that processes the form reads the data, checks to see if the name is empty, and finally sends the data mail to you.
Form: form.php3
Include ("Include/common.inc");
$title = "Feedback";
Include ("Include/header.inc");
?>




Include ("Include/footer.inc");
?>
Process form: SENDFDBK.PHP3
Include ("Include/common.inc");
$title = "Feedback";
Include ("Include/header.inc");
if ($name = = "")
{
Now I'm sick of anonymous messages!
echo "Duh?" How come is anonymous? ";
}
ElseIf ($name = = "Your name")
{
The visitors really don't want to be named!
echo "Hello?" Your nameis supposed to being replaced with
Your actual name!";
}
Else
{
Output a polite thank you language
echo "
Hello, $name.


Thank for your feedback. It is greatly appreciated.


Thanking


$MyName

$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 configuration file (PHP3 is PHP3.INI,PHP4 to php.in) is not set up properly. Because this program requires your PHP configuration file to do the following settings:
First, open your Php3.ini or php.ini file with Notepad, and check to see if [Mail function] is set up, the default is as follows:
SMTP = localhost
Sendmail_from = me@localhost.com
Set SMTP server to SMTP, preferably your local SMTP server, I use the 21CN SMTP server as an example, and then, in Sendmail_from place your e-mail address, for example, can be changed to this:
SMTP = smtp.21cn.com
Sendmail_from = pert@21cn.com
Don't forget to restart the Apache,iis or PWS service after the change.
5.7 Simple in-site search engine
PHP can call external programs. In the UNIX environment we can use the program grep to implement a simple search engine. What we can do is a little more complicated: use a single page to output a form for the user to enter the search string and output the query results.
Include ("Include/common.inc");
$title = "Search";
Include ("Include/header.inc");
?>




if (! empty ($SEARCHSTR))
{
Empty () to check if the query string is empty
If not empty, call grep query
echo "n";
Call grep to make a case insensitive query for all files in a non-sensitive mode
$cmdstr = "Grep-i $searchstr *";
$fp = Popen ($cmdstr, "R"); Executing commands and outputting pipelines
$myresult = Array (); Store query Results
while ($buffer = FGETSS ($fp, 4096))
{
grep returns the format: file name: Number of rows in the matching string
So we use the 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 that we have the results stored in the array, we can process and output the
if (count ($myresult))
{
echo "
      n ";
      while (list ($fname, $fline) = each ($myresult))
      echo "

    1. $fname: $fline
    2. n ";
      echo "
n ";
}
Else
{
If no query results
echo "Sorry. Search on $searchstr
Returned no results.
n ";
}
Pclose ($FP);
}
?>
Include ("Include/footer.inc");
?>
Comments:
Php_self is a PHP built-in variable. Contains the current file name.
Fgets () reads a file by line, up to 4096 (specified) character length.
FGETSS () is similar to Fgets (), only the HTML markup that parses the output.
Split () has a parameter of 2, because we only need to divide the output into two parts. In addition, ":" needs to be omitted.
Each () is an array manipulation function that makes it easier to traverse the entire array.
The functions of Popen (), Pclose () and fopen (), fclose () are similar, but only the pipeline processing is added.
Please note that the above code is not a good way to implement a search engine. This is just one example of what helps us learn PHP better. Ideally, you should create a database that contains keywords and then search.

The above introduces the novice to start the road to the new PHP road (vi), including the driver to start the road aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.