Home  >  Article  >  Backend Development  >  Detailed explanation of steps to staticize pages using PHP

Detailed explanation of steps to staticize pages using PHP

php中世界最好的语言
php中世界最好的语言Original
2018-05-19 10:38:471808browse

This time I will give you a detailed explanation of the steps to use PHP page staticization. What are the precautions for using PHP page staticization? Here are practical cases, let’s take a look.

Page staticization, as the name suggests, is to convert dynamic PHP into static Html. The process is as follows

When the user accesses index.php, if index exists. html and within the validity period, directly output index.html, otherwise generate index.html

file_put_contents() output static file

ob_start() open PHP buffer

ob_get_contents() Get the buffer contents

ob_clean() Clear the buffer

ob_get_clean() 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('user', ['uid', 'username', 'email']);
 // 引入模板
 require_once "./templates/index.php";
 // 写入html
 file_put_contents('./html/index.html', ob_get_contents());
}

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the steps to implement dependency injection using PHP class reflection

How to delete files in the directory using PHP unlink and rmdir accomplish

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

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