Implementing Dynamic Web pages with PHP

Source: Internet
Author: User
Tags ftp functions variables php file php and php code variable how to use ftp
Dynamic | Web dynamic information theory
According to Merriam-webster Online, the term "dynamic" means that the content of a Web page is composed of "usually continuous and real-time generated activities or changes of information." "So when we talk about dynamic data, we're talking about the information sent to the user as a Web page is a combination of different source data." This is relative to the concept of static Web pages, the content of static Web pages does not depend on the data entered by the user and is usually resolved directly to the user. There are three main types of dynamic information on a Web page:

Dynamic Data-A variable that is generated in a Web page.
Dynamic Web pages--The entire Web page is dynamically generated.
Dynamic content--web Part of the page is dynamically generated.
If you want to have subtle control over the processes that are generated by dynamic content, like the type of dynamic data, the process of data processing can be more complex. 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 generation of dynamic content is a tradeoff between these two ways, which allows us to use two very useful PHP functions, include () and require ().

Keep in mind that the more logic you add to your backend, the more your Web site's performance will be lost. Luckily, PHP is very fluent in the process of pre processing, so I use PHP as much as I can with dynamic content and data.

Data Sources and PHP features
All dynamic content has one thing in common: they come from a data source other than a raw page. Figure A lists some common data sources and the corresponding PHP functions that are used to process 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)
<dbtype>_connect ()
<dbtype>_pconnect ()
<dbtype>_close ()
<dbtype>_<function> ()
Example
Mysql_fetch_array ()

These are just some of PHP's many database access functions, many of which are specially written for each of the different databases. 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 work with data from files located on the local server, such as configuration files.

Common data sources and PHP functions to handle them

In this article "Tutorial: PHP Start," We watched a demo script that asked users to enter their favorite numbers. Based on the results of the user input, 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 will determine what is displayed. A more complex example is the "click Process" Application, which is able to decide which ads to send to him or her based on a page that a user has visited on a Web site.

Once the data has been entered, whether it was entered by the user or otherwise, it will be saved in a database and reused later. If it is used to determine what is displayed, 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 a simple PHP script example of a file-driven dynamic content. We will use the logic based on a configuration file to determine what page styles and fonts should be displayed on a Web page. The page style we choose will be displayed when the user requests a Web page. (Here I would like to give you a wake-up call for examples that include files: 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 variable values and several include files that contain portions of the variable containing HTML. While this may not seem particularly dynamic, you can easily ask the user to create a profile using the Web table forms and use certain logic to determine which profile to load, and so on. (Our discussion in the "Understanding PHP Functions and Classes" article) will help you do this work. )

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

Table A

This simple code has to do three things:


Use the PHP include () function to include the <!--display.php the style of this web page is determined by a configuration file-->
<title>mood page</title>
<?php
Include ("displayconf.php");
$required _file = $display. ". PHP ";
Require $required _file;
?>
<br><br>
<center>this is the best "mood page" ever!</center>
</font>
</body>

The variables in the displayconf.php and evaluate them.
Creates a variable that represents the file name requested by the user. In our case, 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 example, the PHP require () function and the include () function are completely interchangeable. The main difference between the two functions is that the target file is handled differently. A require () statement will be replaced by the file that it calls. This means that in one loop, the remote file is only invoked once. On the other hand, each time the include () function is encountered, it is evaluated again. This means that during a loop, the file will be accessed once during each loop, and the variables set in the file will be updated each time.

In this case, I'm trying to figure out when to use what functions are 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. So, I chose the include () function. On the other hand, $required files are likely to not change in the process of interacting. 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 better control the management of file processing and configuration file variables.

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

Table B

<?php# displayconf.php
# display.php configuration file
# -------------------------------------------------
# set the variable $display to one of the following values:
# happy, sad, or generic
$display = "Happy";
?>
Finally we need some content files--each option in the corresponding configuration file. Because these are static HTML, we don't need to add PHP pins 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 to the end of the process.

"Happy" File contents (happy.php)
<body Bgcolor=pink text=yellow>
<font size= "+5" >

"Sad" File contents (sad.php)
<body Bgcolor=blue text=white>
<font face= "Arial, Helvetica" size= "+5" >

"Generic" File contents (generic.php)
<body Bgcolor=white text=black>
<font face= "Courier" Size= "+5" >

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

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

Here are some final words. While I'm sure you're familiar with the Wai Web Programming Guide, you might also want to look at what the consortium says about dynamic content and how users can 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.