Home php教程 php手册 谈PHP生成静态页面分析 模板+缓存+写文件

谈PHP生成静态页面分析 模板+缓存+写文件

Jun 13, 2016 pm 12:22 PM
php one superior Write analyze dynamic exist document template generate cache speed static page

一、引 言
在速度上,静态页面要比动态页面的比方php快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权限限制等,但是,对应一些我们经常频频使用的文件,比方说,开发的新闻发布系统,我们不希望很多用户都读取数据库才显示结果,这样一方面消耗了服务器的资源,另一方面占去了浏览者大量可贵的响应时间,所有,有了"静态页面话"的做法,当前很多网站都采用这种技术,一般都是由管理后台控制,或者生成html直接显示,或者xhtml用css控制显示,或者生成xml用xslt显示,这些技术都不是难的,在这里我就浅显的说说生成html的方法.
二、预备知识
模板技术:
[PHP] 模板引擎Smarty深入浅出介绍 --2005-12-31
[PHP] 笑谈配置,使用Smarty技术 --2006-01-04
缓存技术:
有些信息比方经常不变的,但是还是能变的信息放在缓存中以加快显示速度,这是很有价值的,所谓的缓存,通俗的理解就是一些保存在服务器端的共用信息.它是于服务器同生死的,我们在保存缓存的时候可以指定下次更新的时间的判断,比方要在5分钟更新一次,可以记录上次更新的时间,和当前时间比较,如果大于 5 分钟 ,读取数据库,更新换成,否则直接读取缓存数据,当然,缓存需要客户端用户激活的,只需一次.
ob_start()函数:打开输出缓冲区.
函数格式 void ob_start(void)
说明:当缓冲区激活时,所有来自PHP程序的非文件头信息均不会发送,而是保存在内部缓冲区。为了输出缓冲区的内容,可以使用ob_end_flush()或flush()输出缓冲区的内容。
Flush:刷新缓冲区的内容,输出。
函数格式:flush()
说明:这个函数经常使用,效率很高。
ob_get_contents :返回内部缓冲区的内容。
函数格式:string ob_get_contents(void)
说明:这个函数会返回当前缓冲区中的内容,如果输出缓冲区没有激活,则返回 FALSE.
ob_get_length:返回内部缓冲区的长度。
函数格式:int ob_get_length(void)
说明:这个函数会返回当前缓冲区中的长度;和ob_get_contents一样,如果输出缓冲区没有激活,则返回 FALSE.
ob_end_clean:删除内部缓冲区的内容,并且关闭内部缓冲区
函数格式:void ob_end_clean(void)
说明:这个函数不会输出内部缓冲区的内容而是把它删除
ob_end_flush:发送内部缓冲区的内容到浏览器,并且关闭输出缓冲区
函数格式:void ob_end_flush(void)
说明:这个函数发送输出缓冲区的内容(如果有的话)
ob_implicit_flush:打开或关闭绝对刷新
函数格式:void ob_implicit_flush ([int flag])
说明:默认为关闭缓冲区,打开绝对输出后,每个脚本输出都直接发送到浏览器,不再需要调用 flush()
文件写入:
int fwrite ( resource handle, string string [, int length] )
fwrite() 把 string 的内容写入 文件指针 handle 处。 如果指定了 length,当写入了 length 个字节或者写完了 string 以后,写入就会停止,视乎先碰到哪种情况。
fwrite() 返回写入的字符数,出现错误时则返回 FALSE 。
相关参考官方网站: 文件参考
三、解决方案
思路:开启 ob_start缓冲,当已经调出数据的时候获取 ob_get_contents,然后生成静态页,ob_end_clean清除缓冲.ok,就这么来,来看一个例子(php+mysql的结合):
创建数据库:

复制代码 代码如下:


Create TABLE `bihtml` (
`id` int(11) NOT NULL auto_increment,
`szdtitle` varchar(16) NOT NULL,
`szdcontent` text NOT NULL,
PRIMARY KEY (`id`)
) TYPE


获取当前的ID,并导入模板:

复制代码 代码如下:


ob_start();
$id=_POST['id']
if(!isset($id)&is_integer($id))
{
@$db=new mysqli('localhost','root','admin','bihtml');
$result=$db->fetch_one_array("select * from szd_bi where id='$id'");
if(!emptyempty($result))
{
$tmp->assign(array(
"Szdtitle",htmlspecialchars($result['titles']),
"Szdcontent",$result['titles']));
}
$tpl->display('default_1.tpl');
$this_my_f= ob_get_contents(); //此处关键
ob_end_clean();
$filename = "$id.html";
if(tohtmlfile_cjjer($filename,$this_my_f))
echo "生成成功 $filename";
else
echo "生成识别";
}
}

//把生成文件的过程写出函数
function tohtmlfile_cjjer($file_cjjer_name,$file_cjjer_content)
{
if (is_file ($file_cjjer_name)){
@unlink ($file_cjjer_name);
}
$cjjer_handle = fopen ($file_cjjer_name,"w");
if (!is_writable ($file_cjjer_name)){
return false;
}
if (!fwrite ($cjjer_handle,$file_cjjer_content)){
return false;
}
fclose ($cjjer_handle); //关闭指针
return $file_cjjer_name;
}


四、说明事项
1: 一般建议管理员添加数据的时候就生成静态页面,可以考虑记录生成的文件名次和路径.
2: php主要是 ob_starts()和 ob_get_contents,生成静态页面的时候很有用,当然也可以考虑调出数据库直接替换模板里面的变量也是可以的.
3: 主要的模板使用smarty,phplib都是可以的,smarty使用比较简易.
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 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles