PHP beginners (5)

Source: Internet
Author: User

Building a simple interaction website (1)

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.

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

5.2 modularization with include and require

Let's look at some websites built on PHP. Almost every page of the website contains include and require embedded in the PHP file. This is because the use of include and require not only enhances the readability of the code, but also can be divided into modules to manage the site. In general, there must be repeated content for each page on a website. For example, the navigation bar of the page header, the advertisement icon, or the side navigation bar. There may also be copyrights or text-based navigation entries at the bottom of the footer of each page. If we want to modify the navigation bar or logo of a large website containing thousands or hundreds of pages, we can only change each page one by one in our previous methods. I don't need to say that, you can imagine how difficult and painful this is. Is there any better solution? The answer is yes. We can put all repeated content in a file, and then use the include and require functions of PHP to dynamically call the file on every page that requires the content. In this way, if we want to modify the reusable content on all pages, we only need to change the files that contain the Duplicated content.

For your understanding, let's take a look at a simple application of include and require:

Starting from the HTML page, you may also include the header (head.htm) of each page on the site ).
<HTML>
<HEAD>
<TITLE> my personal homepage </TITLE>
</HEAD>
<BODY>

Page Content (content.htm ).
<H1>
Welcome to my cold house, although there is nothing at the moment.
</H1>

Links of HTML pages (trail.htm)
</BODY>
</HTML>

Use the include and require functions to separate HTML from PHP and divide HTML and PHP into modules:
<?
/*
Call the header of an HTML page
*/
Require ("head.htm ");
/*
Call the HTML page content
*/
Require ("centent.htm ");
/*
Call the end Of the HTML page
*/
Require ("trail.htm ");
?>

5.3 let's start with a title page, a contact information page, and a resume page. We also need standard and common page headers and bottoms.

Webpage --front.htm
Here we have a very simple html file:
<HTML>
<HEAD>
<TITLE>
My personal homepage -- welcome
</TITLE>
</HEAD>
<BODY>
<H1>
My personal homepage
</H1>
<H2>
Welcome
</H2>
<HR>
<P>
Welcome to my cold house, although there is nothing at the moment.
</P>
<P>
But I hope I can add more immediately.
</P>
<HR>
<P align = "CENTER">
<SMALL> <I>
Copyright? Me, 1999
</I> </SMALL>
</P>
</BODY>
</HTML>

Contact information page --count.htm
We also have a simple page:
<HTML>
<HEAD>
<TITLE>
My personal homepage-contact information
</TITLE>
</HEAD>
<BODY>
<H1>
My personal homepage
</H1>
<H2>
Contact information
</H2>
<HR>
<P>
You can contact me through 1-800-PHP-INFO
</P>
<HR>
<P align = "CENTER">
<SMALL> <I>
Copyright? Me, 1999
</I> </SMALL>
</P>
</BODY>
</HTML>


5.4 from 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. The include and require functions contain a PHP code file. Regardless of the file extension, the file is treated as a PHP file. We will put these include files under a subdirectory named include and obtain the files suffixed with. inc. 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 = "<a href =" mailto: $ MyEmail "> $ MyEmail </a> ";
$ MyName = "PHP Talk ";
$ MySiteName = $ MyName. "'s Home Page ";
?>

General page header: header. inc
<?
// Define the general page header
?>
<HTML>
<HEAD>
<TITLE>
<? Echo "$ MySiteName-$ title";?>
</TITLE>
</HEAD>
<BODY>
<H1>
<? Echo "$ MySiteName";?>
</H1>
<H2>
<? Echo "$ title";?>
</H2>
<HR>

General page bottom: footer. inc
<?
// At the bottom of the general page
?>
<HR>
<P align = "CENTER">
<SMALL> <I>
Copyright? By
<? Echo "$ MyName ($ MyEmailLink)";?>
, 1999
</I> </SMALL>
</P>
</BODY>
</HTML>

New Page front. php3:
<?
Include ("include/common. inc ");
$ Title = "Welcome ";
Include ("include/header. inc ");
?>
<P>
Welcome to my cold house, although there is nothing at the moment.
</P>
<P>
But I hope I can add more immediately.
</P>
<?
Include ("include/footer. inc ");
?>

New count. php3:
<?
Include ("include/common. inc ");
$ Title = "Contact Information ";
Include ("include/header. inc ");
?>
<P>
You can contact me through 1-800-PHP-INFO
</P>
<?
Include ("include/footer. inc ");
?>

Now you can understand 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.

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.