PHP beginners (6)

Source: Internet
Author: User
Tags php3 file
PHP beginners (6) building a simple interactive website (2)

5.5 counters

Let's add a counter to the homepage. This example has been mentioned 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)
{
/* Returns the counter to zero.
In this case, if the counter is not used, the initial value is 1.
Of course, you can set the initial value to 20000 to cheat me.
*/
$ Count = 0;
// If the stored counter file already exists, read the content
If (file_exists ($ counter_file ))
{
$ Fp = fopen ($ counter_file, "r ");
// We only took the first 20 digits. I hope your site will not be too popular.
$ Count = 0 + fgets ($ fp, 20 );
// Because the function fgets () returns a 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 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 counter.txt file, read 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 another feedback form so that your browser can enter it and email it to you. For example, we use a very simple method to implement it. we only need two pages: one to provide an input form for the viewer, and the other to obtain the form data and process and mail it to you.

It is very easy to obtain form data in PHP. After a form is sent, each element in the form is assigned a value, which can be used like a common variable.


In process_form.php3, the variable $ mytext is assigned the input value-very simple! Similarly, you can obtain variable values from form elements such as the list box, multiple-choice box, single-choice, and Button. The only thing you have to do is name every element in the form so that it can be referenced in the future.

Based on this method, we can generate a simple form containing three elements: name, email address, and message. After a browser sends a form, the browser reads data from the PHP page (sendfdbk. php3) of the form, checks whether the name is blank, and finally mails the data to you.

Form: form. php3
Include ("include/common. inc ");
$ Title = "Feedback ";
Include ("include/header. inc ");
?>




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 come you are anonymous? ";
}
Elseif ($ name = "Your name ")
{
// This viewer really does not want to be named!
Echo "Hello? Your nameIs supposed to be replaced
Your actual name!";
}
Else
{
// Output a polite thank-you message
Echo"
Hello, $ name.


Thank you for your feedback. It is greatly appreciated.


Thanking you


$ MyName

$ MyEmailLink
";
// Finally mail it out
Mail ($ MyEmail, "Feedback .","
Name: $ name
Email: $ email
Comment: $ comment
");
}
Include ("include/footer. inc ");
?>

Note: If the program works properly during your testing, check whether your PHP configuration file (PHP3 is php3.ini and PHP4 is php. in) is set. This program requires the following settings for your PHP configuration file:

First, use NotePad to open your php3.ini or php. ini file and check whether [mail function] has been set. the default situation is as follows:
SMTP = localhost
Sendmail_from = me@localhost.com
Set SMTP server for SMTP, preferably your local SMTP server. here I will use the SMTP server of 21cn as an example, and then fill in your E-MAIL address in 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 after modification.


5.7 simple intra-site search engine

PHP can call external programs. In Unix, we can use the program grep to implement a simple search engine. We can make it a little more complicated: using a page to output a form for users to enter search strings and output query results.

Include ("include/common. inc ");
$ Title = "Search ";
Include ("include/header. inc ");
?>




If (! Empty ($ searchstr ))
{
// Empty () is used to check whether the query string is null.
// If it is not null, call grep to query
Echo "n ";
// Call grep to query all files in case-insensitive mode
$ Response str = "grep-I $ searchstr *";
$ Fp = popen ($ Pipeline Str, "r"); // execute the command and output the pipeline
$ Myresult = array (); // store the query results
While ($ buffer = fgetss ($ fp, 4096 ))
{
// Grep returns the following format: File name: number of lines in the matched string
// Therefore, we use the split () function to separate and process data.
List ($ fname, $ fline) = split (":", $ buffer, 2 );
// We only output the first matching result.
If (! Defined ($ myresult [$ fname])
$ Myresult [$ fname] = $ fline;
}
// Now we store the results in an array, and we can process and output the result below.
If (count ($ myresult ))
{
Echo"
    N ";
    While (list ($ fname, $ fline) = each ($ myresult ))
    Echo"

  1. $ Fname: $ fline
  2. N ";
    Echo"
N ";
}
Else
{
// If no query results exist
Echo "Sorry. Search on $ Searchstr
Returned no results.
N ";
}
Pclose ($ fp );
}
?>
Include ("include/footer. inc ");
?>


Note:

PHP_SELF is a built-in PHP variable. Contains the current file name.
Fgets () reads files by row. The maximum length is 4096 (specified) characters.
Fgetss () is similar to fgets (), but only parses the HTML tag of 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 ":".
Each () is an array operation function, which is used to traverse the entire array more conveniently.
Popen () and pclose () are similar to fopen () and fclose (), but MPs queues are added.
Note that the above code is not a good way to implement a search engine. This is just an example that helps us better learn PHP. Ideally, you should create a database containing keywords and search for them.

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.