search
HomeBackend DevelopmentPHP Tutorial使用PHP实现动态网页_PHP

使用PHP实现动态网页_PHP

Jun 01, 2016 pm 12:38 PM
usecontentfunctiondynamicaccomplishusdocumentWeb page

      今天,Web站点费尽心思想要为用户提供他们从未体验过的东西。除了友好的界面,细致的服务,实用的信息以外,为用户定制的动态的网页内容也能够提高Web站点的用处并加强访问的针对性,这使得用户更有可能在以后再次访问你的网站。在这篇文章里,我们将从对动态信息的一个概述开始。然后我们将解释如何使用PHP来在Web页面里创建动态的内容并察看一个演示的例子。

动态信息理论

据Merriam-Webster Online地解释,动态一词的意思是网页的内容由“通常是连续的和实时生成的活动或者改变的信息组成。”因此当我们谈论动态数据的时候,我们也就是在谈论作为Web页面发送给用户的信息是由不同的源数据组合而成的。这与静态网页的概念是相对的,静态网页的内容并不依赖用户输入的数据而改变而且通常是直接解析给用户。Web页面上的动态信息有三种主要的类型:

  • 动态数据--在一个Web页面里产生的变量。
  • 动态Web页面--整个Web页面都是动态生成的。
  • 动态内容--Web页面的一部分是动态生成的。

你如果希望对动态内容产生的过程进行细微的控制,就象动态数据的那种类型一样,那么数据处理的过程就会复杂一些。而如果你想生成大范围的信息,就象动态Web页面的生成那样,那么程序的逻辑就会变得复杂。动态内容的生成是这两种方式的折衷,它能够让我们使用两个很有用的PHP函数,include()和require()。

要记住,你在后端加入的逻辑越多,那么你的Web站点的性能将会损失得越严重。幸运的是,PHP能够很流畅的进行预处理过程,所以当我在处理动态内容和数据的时候都尽可能多的使用PHP的功能。

数据源和PHP功能

所有的动态内容都有一个共同点:它们从一个原始页面以外的数据源而来。图A列出了一些常见的数据源以及用来处理它们的相应的PHP函数。

Figure A

数据源
PHP 函数
注释
User
$HTTP_POST_VARS
$HTTP_GET_VARS

这些函数处理由用户通过Web表单直接输入的数据。
Database (local or remote)

_connect()
_pconnect()
_close()
_()
example:
mysql_fetch_array()

这些都只是PHP许多数据库访问函数中的一部分,许多函数是为每个不同的数据库特别编写的。你能够在PHP函数参考手册里找到这些函数的完整列表。
Remote file
fopen(), fclose()
fgets(), fputs()

这些函数处理一个远程服务器上文件中的数据,这个文件可以通过FTP访问。
Local file
include(), require()
fopen(), fclose()

这些函数处理位于本地服务器上的文件中的数据,比方说配置文件。
常见的数据源和处理它们的PHP函数

在这篇文章“教程:PHP起步中,”我们观看了一个演示的脚本,这个脚本要求用户输入他们最喜欢的数字。根据用户输入的结果,我们在Web页面上显示一条消息。这就是一个由用户驱动的动态Web内容的例子。从Web表单返回的结果将决定显示的内容。一个更复杂的例子是“点击流程”应用程序,这个程序能够根据一个用户在Web站点上访问过的页面来决定向他或者她发送什么广告。

一旦数据已经输入,不管它是由用户或者其它的方式输入的,将会被保存在一个数据库中并在以后重新使用。如果它被用来决定显示的内容,那么这些内容将可以被认为是“由数据库驱动的动态内容。”我们将在下一篇文章中更仔细的看看这种类型的动态信息。 目前,让我们先察看一个由文件驱动的动态内容的简单的PHP脚本的例子。我们将使用基于一个配置文件的逻辑来决定在Web页面上应该显示什么样的页面风格和字体。我们选择的页面风格将会在用户请求Web页面的时候显示出来。(这里我想就包括文件的例子给你提个醒:你真的应该在这个例子中为完成要求的功能而使用风格页。)

例子程序:Display.php

 

Display脚本使用一个独立的配置文件来包含变量值和几个含有HTML的变量部分的包括文件。虽然这看起来不是特别动态,但是你能够轻易的要求用户使用Web表单来创建一个配置文件并使用一定的逻辑来判断应该加载哪一个配置文件,等等。(我们在“理解PHP的函数和类”这篇文章中所作的讨论将帮助你完成这个工作。)

由于本文的目的所限,我们将跳过这方面的处理过程并尽量使它简化。表A展示了我们的主页面,以及你通过浏览器调用的页面,Display.php。(PHP代码将用粗体进行显示。)

表A

这段简单的代码必须做三件事情:

  • 使用PHP include()函数来包含
    <!-- display.php 这个Web页面的风格由一个配置文件决定 --> 
     
     
    <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>
    
    
    
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Give an example of how to store a user's name in a PHP session.Give an example of how to store a user's name in a PHP session.Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.