Home > Backend Development > PHP Tutorial > Detailed explanation of examples of static PHP pages

Detailed explanation of examples of static PHP pages

php中世界最好的语言
Release: 2023-03-20 19:06:01
Original
1469 people have browsed it

Page staticization, as the name suggests, is to convert dynamic PHP into static Html. In the following article, the editor will introduce the principles and related methods of PHP page staticization. Friends in need can refer to it.

The specific process is as follows

#The user accesses index.php. If index.html exists and is within the validity period, index.html will be output directly. , otherwise generate index.html

file_put_contents() output static file

ob_start() turn on the PHP buffer

ob_get_contents() get the buffer Content

ob_clean() clears the buffer

ob_get_clean() is equivalent to ob_get_contents()+ob_clean()

Code Example

<?php

if (file_exists(&#39;./html/index.html&#39;) && time() - filectime(&#39;./html/index.html&#39;) < 30) {
 require_once &#39;./html/index.html&#39;;
} else {
 // 引入数据库配置
 require_once "./config/database.php";
 // 引入Medoo类库
 require_once "./libs/medoo.php";
 // 实例化db对象
 $db = new medoo($config);
 // 获取数据
 $users = $db->select(&#39;user&#39;, [&#39;uid&#39;, &#39;username&#39;, &#39;email&#39;]);
 // 引入模板
 require_once "./templates/index.php";
 // 写入html
 file_put_contents(&#39;./html/index.html&#39;, ob_get_contents());
}
Copy after login

Related recommendations:

php’s caching mechanism to realize page static code sharing

PHP page Static implementation code

php Recommended video tutorial materials for page staticization


The above is the detailed content of Detailed explanation of examples of static PHP pages. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template