Home > Article > Backend Development > Detailed explanation of steps to staticize pages using PHP
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('./html/index.html') && time() - filectime('./html/index.html') < 30) { require_once './html/index.html'; } 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!