3. start from the instance

Source: Internet
Author: User
Tags php3 file
3. start from the instance. 3. PHP practices


Many features of PHP are related to other software or tools. Using the PHP knowledge we have learned so far, we can try to build a simple interaction website. We can learn a lot from this process. Well, we are now focusing on the construction of a typical personal website.



3.1 plan a site

Generally, a personal site includes a welcome page, a message book page, a bookmarkdonurl page, a counter, contact information, and even a photo set and some music files. Let's start with a title page, a contact information page, and a resume page. We also need standard generic page headers and bottoms.



Webpage --front.html





Here we have a very simple html file:





<Br> my personal homepage -- welcome <br>







My personal homepage





Welcome







Welcome to my cold house, although there is nothing at the moment.





But I hope I can add more immediately.









Copyright? Me, 1999











Contact Information page --count.html



We also have a simple page:





<Br> my personal homepage -- Contact Information <br>







My personal homepage





Contact Information







You can contact me through 1-800-PHP-INFO









Copyright? Me, 1999













3.2 HTML to PHP



As you can see above, each page has the same header and bottom. As shown in the preceding figure, the same information can be written to each page when the workload is low, but imagine how much effort you will spend when there are more than 100 pages and you need to change all of their headers or bottoms? How tedious and boring is manual changes on one page! Therefore, we should write PHP headers and bottom files for these pages, and then we just need to reference them in each HTML page. We will put these include files under a subdirectory named include. Next we will write the general content of these sites into the file.



General site variable settings: common. inc


// General site variables

$ MyEmail = "phptalk@tnc.org ";

$ MyEmailLink = "$ MyEmail ";

$ MyName = "PHP Talk ";

$ MySiteName = $ MyName. "'s Home Page ";

?>



General page header: header. inc


// Define the general page header

?>





<Br> <? Echo "$ MySiteName-$ title";?> <Br>





















General page bottom: footer. inc


// At the bottom of the general page

?>







Copyright? By



, 1999











New page front. php3:


Include ("include/common. inc ");

$ Title = "Welcome ";

Include ("include/header. inc ");

?>



Welcome to my cold house, although there is nothing at the moment.





But I hope I can add more immediately.




Include ("include/footer. inc ");

?>



New cont. php3:


Include ("include/common. inc ");

$ Title = "Contact Information ";

Include ("include/header. inc ");

?>



You can contact me through 1-800-PHP-INFO




Include ("include/footer. inc ");

?>



Now you can guess the benefits of this arrangement. If you want to change the header or bottom of the page, you only need to change the corresponding file. If you want to modify your e-mail address or even your name, you just need to modify the common. inc file. In addition, you can include files with any file name or extension in your file, and you can even include files on other sites.



3.3 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



3.4 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 ");

?>



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