Home Backend Development PHP Tutorial PHP generates static pages_PHP tutorial

PHP generates static pages_PHP tutorial

Jul 13, 2016 am 10:58 AM
php post exist place article method friend generate of system static page

I saw many friends posting in various places asking how to generate a static article system in PHP. I have built such a system before, so I would like to share my opinions for your reference. Okay, let's review some basic concepts first.

1. PHP scripts and dynamic pages.

PHP script is a server-side script program that can be mixed with HTML files through methods such as embedding, or it can process user requests in the form of templates in the form of classes, function encapsulation, etc. One way or another, the basics of it are this. The client makes a request for a certain page -----> The WEB server introduces the designated corresponding script for processing -----> The script is loaded into the server -----> PHP parsing specified by the server The script is parsed by the browser to form HTML language form----> The parsed HTML statement is sent back to the browser in the form of a package. It is not difficult to see from this that after the page is sent to the browser, PHP no longer exists and has been converted and parsed into HTML statements. The client request is a dynamic file. In fact, there is no real file there. PHP parses it into the corresponding page and then sends it back to the browser. This way of handling pages is called "dynamic pages".

2. Static pages.

Static pages refer to pages that actually exist on the server side and only contain HTML and JS, CSS and other client-side scripts. The way it is handled is. The client makes a request for a certain page----> The WEB server confirms and loads a certain page----> The WEB server passes the page back to the browser in the form of a package. From this process, we can compare the dynamic pages and we can see. Dynamic pages need to be parsed by the PHP parser of the WEB server, and usually need to connect to the database and perform database access operations before they can form an HTML language information package; while static pages do not need to be parsed or connected to the database, and can be sent directly, which can greatly Reduce server pressure, improve server load capacity, and greatly improve page opening speed and overall website opening speed. But its disadvantage is that the request cannot be processed dynamically, and the file must actually exist on the server.

3. Templates and template analysis.

The template is an html file that has not yet been populated with content. For example:

temp.html


{ title}

This is a { file} file's templets



PHP processing:

templetest.php

$title = "Webpage Teaching Network Test Template";
$file  = "Webjx test templet,
author: web@webjx.com";

$fp= fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{ file}",$file,$content);
$content .= str_replace ("{ title}",$title,$content);

echo $content;
?>



Template parsing processing is the process of filling (content) into the template with the results obtained after the PHP script parsing and processing. Usually with the help of template classes. Currently, the more popular template parsing classes include phplib, smarty, fastsmarty and so on. The principle of template parsing processing is usually replacement. There are also some programmers who are accustomed to putting judgment, loop and other processing into template files and using analysis classes for processing. The typical application is the block concept, which is simply a loop processing. The PHP script specifies the number of loops, how to loop through, etc., and then the template parsing class implements these operations.

Okay, after comparing the advantages and disadvantages of static pages and dynamic pages, now let’s talk about how to use PHP to generate static files.

PHP generating static pages does not refer to PHP's dynamic parsing and outputting HTML pages, but refers to using PHP to create HTML pages. At the same time, because HTML is not writable, if the HTML we create is modified, it needs to be deleted and regenerated. (Of course, you can also choose to use regular rules to modify it, but I personally think that it is faster than deleting and regenerating it. The gain outweighs the loss.)

Back to business. PHP fans who have used PHP file operation functions know that there is a file operation function fopen in PHP, which opens a file. If the file does not exist, try to create it. This is the theoretical basis on which PHP can be used to create HTML files. As long as the folder used to store HTML files has write permission (ie permission definition 0777), the file can be created. (For UNIX systems, Win systems do not need to be considered.) Taking the above example as an example, if we modify the last sentence and specify to generate a static file named test.html in the test directory:

$title = "Webpage Teaching Network Test Template";
$file  = "Webjx test templet,
author: web@webjx.com";

$fp   = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{ file}",$file,$content);
$content .= str_replace ("{ title}",$title,$content);

// echo $content;

$filename = "test/test.html";
$handle = fopen ($filename,"w"); //Open the file pointer and create the file
/*
Check if the file is created and writable
*/
if (!is_writable ($filename)){
die ("File: ".$filename." is not writable, please check its properties and try again!");
}
 if (!fwrite ($handle,$content)){ //Write information to file
  die ("Generate file".$filename."Failed!");
}
fclose ($handle); //Close pointer

die ("Create file".$filename."Success!");
?>

Reference for solutions to common problems in practical applications:

1. Article list question:

Create a field in the database and record the file name. Each time a file is generated, the automatically generated file name is stored in the database. For recommended articles, just point to the page in the designated folder where the static file is stored. Use PHP operations to process the article list, save it as a string, and replace this string when generating the page. For example, add the mark {articletable} to the table where the article list is placed on the page, and in the PHP processing file:

$title = "Webpage Teaching Network Test Template";
$file  = "Webjx test templet,
author: web@webjx.com";

$fp   = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content .= str_replace ("{ file}",$file,$content);
$content .= str_replace ("{ title}",$title,$content);

//Start generating list
$list = '';
$sql = "select id, title,filename from article";
$query = mysql_query ($sql);
while ($result = mysql_fetch_array($query)){
$list .= ''.$result['title'].'
';

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632014.htmlTechArticleI saw many friends posting in various places asking how to generate a static article system in PHP. I have built such a system before , so I would like to share some opinions for your reference. Okay, let’s review it first...
Statement of this Website
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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles