Using PHP to implement Dynamic Web _php

Source: Internet
Author: User
Keywords Web dynamic implementation use file us one function content PH
Tags file handling how to use ftp
Today, Web sites are trying to provide users with things they have never experienced before. In addition to the friendly interface, meticulous service, practical information, customized dynamic Web content for users can also improve the usefulness of Web sites and enhance access to targeted, which makes users more likely to visit your site in the future. In this article, we'll start with an overview of dynamic information. Then we'll explain how to use PHP to create dynamic content in a Web page and see an example of a demo.

Dynamic information theory

According to Merriam-webster Online, the term "dynamic" means that the content of a Web page consists of "activities that are usually continuous and real-time generated or changed." "So when we talk about dynamic data, we're talking about the fact that the information sent to the user as a Web page is composed of different source data." This is relative to the concept of a static Web page, where the content of a static Web page is not dependent on the data entered by the user and is usually parsed directly to the user. There are three main types of dynamic information on a Web page:

    • Dynamic Data--variables generated in a Web page.
    • Dynamic Web pages--The entire Web page is dynamically generated.
    • Dynamic content--web Part of the page is generated dynamically.

If you want to have subtle control over the process of dynamic content, just like the type of dynamic data, the process of data processing can be complicated. And if you want to generate a wide range of information, like the generation of Dynamic Web pages, then the logic of the program becomes complex. The creation of dynamic content is a tradeoff between these two approaches, which allows us to use two useful PHP functions, include () and require ().

Keep in mind that the more logic you add to your backend, the more you will lose the performance of your Web site. Fortunately, PHP is very fluent in pre-processing, so when I work with dynamic content and data, I use PHP as much as I can.

Data Sources and PHP features

All dynamic content has one thing in common: they come from a data source other than the original page. Figure A lists some common data sources and the appropriate PHP functions to handle them.

Figure A

Data source
PHP functions
Comments
User
$HTTP _post_vars
$HTTP _get_vars
These functions handle data that is entered directly by the user through a Web form.
Database (local or remote)

_connect ()
_pconnect ()
_close ()
_ ()
Example
Mysql_fetch_array ()

These are just a few of the many database access functions in PHP, many of which are specifically written for each different database. You can find a complete list of these functions in the PHP function reference manual.
Remote file
fopen (), fclose ()
Fgets (), fputs ()
These functions process data from a file on a remote server that can be accessed via FTP.
Local file
Include (), require ()
fopen (), fclose ()
These functions handle data in files that reside on the local server, such as configuration files.
Common data sources and PHP functions for handling them

In this article, "Tutorial: Starting PHP," We watched a demo script that required users to enter their favorite numbers. Based on the results entered by the user, we display a message on the Web page. This is an example of a user-driven dynamic Web content. The results returned from the Web form determine what is displayed. A more complex example is the "click Process" Application, which can decide what ads to send to him or her based on a page that a user has visited on the Web site.

Once the data has been entered, whether it is entered by the user or by other means, it will be stored in a database and reused later. If it is used to determine what is displayed, then the content will be considered "dynamic content driven by the database." "We'll take a closer look at this type of dynamic information in the next article. For now, let's look at an example of a simple PHP script with file-driven dynamic content. We will use logic based on a profile to determine what page styles and fonts should be displayed on the Web page. The style of the page we choose will be displayed when the user requests a Web page. (Here I would like to remind you of an example of a file: You really should use the style page in this example to complete the required functionality.) )

Example program: display.php

The display script uses a separate configuration file to contain the variable values and several include files that contain the variable portion of the HTML. While this may not seem particularly dynamic, you can easily ask the user to create a configuration file using the Web table forms and use some logic to determine which profile should be loaded, and so on. (Our discussion in the "Understanding PHP Functions and Classes" article will help you with this work.) )

Due to the purpose of this article, we will skip this process and make it as simple as possible. Table A shows our homepage, and the page you call through the browser, display.php. (The PHP code will be displayed in bold.) )

Table A

This simple code must do three things:

    • Use the PHP include () function to include
         
             Mood Page 
         
          

      The best "Mood page" ever!
      The variables in the displayconf.php and evaluates them.
    • Creates a variable that represents the file name requested by the user. In our example, the variable $display defined in the displayconf.php file is evaluated and then appended with the. php suffix. (This is done by our logic.) )
    • Use the PHP require () function to display the contents of the correct include file.

You should note that in our case, the PHP require () function and the include () function are completely interchangeable. The main difference between the two functions is the way the target file is handled differently. A require () statement will be replaced by the file it calls. This means that in a loop, the remote file is only called once. On the other hand, it will be re-evaluated each time the include () function is encountered. This means that during a loop, the file will be accessed once during each cycle, and the variables set in the include file will be updated every time.

In this example, I try to clarify when to use what function is appropriate. For file displayconf.php, it is possible that the value of the variable inside it has changed. After all, this is a configuration file. Therefore, I chose the include () function. On the other hand, $required files are likely to not change in the process of interaction. If the user requests a different file body, then we might create a new file and then include it, so I use the Require () function.

Advanced users may want to look at the PHP manual to learn more about functions require_once () and include_once () to be able to better control the management of file handling and configuration file variables.

Table B shows our configuration file, displayconf.php. (For the sake of simplification, we will put all the files in the same directory as the Web server.) What we're doing here is to set the $display variable to an optional value.

Table B

# displayconf.php# display.php config file #-------------------------------------------------# Set the variable $display to one of the following values: # happy , sad, or generic$display = "Happy";? >

Finally we need some content files-each of the options in the corresponding configuration file. Because the content is static HTML, we don't need to add the PHP script to the file. When you use the include () or require () function in PHP, the called file is skipped at the beginning of the process and then added at the end of the process.

"Happy" File contents (happy.php)

"Sad" File contents (sad.php)

"Generic" File contents (generic.php)

When you click on the page display.php, the look and feel of the page will change depending on the value that you entered in the configuration file.

Summarize

In this article, we discuss the basics of dynamic information and use a script to create dynamic, file-driven content. In particular, we use the include () and require () PHP functions to extract and send our data.

Here are some final words. Although I believe you must be familiar with the Wai Web Programming Guide, you might also want to look at the dominance of dynamic content and the user's ability to access it. You may also want to look at this chapter in the PHP manual, "Using remote Files" to learn how to use FTP to extract configuration data.

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