码迷,mamicode.com
首页 > Web开发 > 详细

如何使用ob函数输出静态html文件

时间:2017-07-10 23:49:52      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:打开   head   缓冲   inf   chunk   order   ase   []   http   

如何使用ob函数输出静态html文件

1、ob函数介绍

1.1、ob_start — 打开输出控制缓冲

bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )  
此函数将打开输出缓冲。当输出缓冲激活后,脚本将不会输出内容(除http标头外),相反需要输出的内容被存储在内部缓冲区中。
详情参考:http://php.net/manual/zh/function.ob-start.php

1.2、ob_get_contents — 返回输出缓冲区的内容

string ob_get_contents ( void )
只是得到输出缓冲区的内容,但不清除它。
详情参考:http://php.net/manual/zh/function.ob-get-contents.php

1.3、ob_end_flush — 冲刷出(送出)输出缓冲区内容并关闭缓冲

bool ob_end_flush ( void )
这个函数将送出最顶层缓冲区的内容(如果里边有内容的话),并关闭缓冲区。如果想进一步处理缓冲区中的内容,必须在ob_end_flush()之前调用 ob_get_contents(),因为在调用ob_end_flush()后缓冲区内容被丢弃。
详情参考:http://php.net/manual/zh/function.ob-end-flush.php

1.4、ob_flush — 冲刷出(送出)输出缓冲区中的内容

void ob_flush ( void )
这个函数将送出缓冲区的内容(如果里边有内容的话)。如果想进一步处理缓冲区中的内容,必须在ob_flush()之前调用ob_get_contents() ,因为在调用ob_flush()之后缓冲区内容将被丢弃。
此函数不会销毁输出缓冲区,而像ob_end_flush() 函数会销毁缓冲区。
详情参考:http://php.net/manual/zh/function.ob-flush.php

1.5、ob_get_clean — 得到当前缓冲区的内容并删除当前输出缓

string ob_get_clean ( void )
得到当前缓冲区的内容并删除当前输出缓冲区。
ob_get_clean() 实质上是一起执行了 ob_get_contents() 和 ob_end_clean()。
详情参考:http://php.net/manual/zh/function.ob-get-clean.php

1.6、ob_get_flush — 刷出(送出)缓冲区内容,以字符串形式返回内容,并关闭输出缓冲区

string ob_get_flush ( void )
ob_get_flush() 刷出(送出)缓冲区内容,以字符串形式返回内容,并关闭输出缓冲区。
Note: 这个函数与ob_end_flush()相似,不同的是本函数还会以字符串形式返回缓冲区内容。
详情参考:http://php.net/manual/zh/function.ob-get-flush.php

2、如何使用ob()函数来制作html的静态页面

2.1、简单输出html文件

<?php
ob_start(); //打开缓冲区
$info = ‘hello world!!‘;
$file=fopen(‘index.html‘,‘w‘); //打开文件index.html
fwrite($file,$info); //写入信息到index.html
fclose($file); //关闭文件index.html
?>
输出hello到index.html
技术分享
找到index.html,正常输出了设定的内容

2.2、获取数据库信息输出html文件

<?php
require_once ‘coon.php‘;
$sql = "select * from name order by id;";
$result = $link->query($sql);
$arr = array();
while($re = $result->fetch(PDO::FETCH_ASSOC)){ 
$arr[] = $re;

//循环输出内容到html文件
ob_start(); //打开缓冲区 
?>
<!-- 下面是输出的内容 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>循环输出的html内容</title>
</head>
<body>
<table>
<thead>
<tr>
<td>id</td>
<td>name</td>
<td>pwd</td>
</tr>
</thead>
<tbody>
<?php
foreach ($arr as $key => $value) {
echo "<tr>"; 
echo "<td>{$value[‘id‘]}</td>";
echo "<td>{$value[‘name‘]}</td>";
echo "<td>{$value[‘pwd‘]}</td>"; 
echo "</tr>";
}
?>
</tbody>
</table> 
</body>
</html>
<?php
$content = ob_get_contents();//得到当前缓冲区的内容
ob_end_clean();//删除当前输出缓
file_put_contents(‘index2.html‘,$content);//写入文件
?>
输出结果到index2.html

 

 

Output Control 函数有很多,大致就先介绍这几种

 

技术分享

2.3 优化读取方式,确定正确读取指定文件

<?php
	$fileName = ‘index2.html‘;
	$re = file_exists($fileName);//判断文件是否存在
	$dValue = 0;
	if($re){
		$fileTime = filectime($fileName);//时间戳
		$dValue = time() -  $fileTime;//获取创建时间,文件缓存一般存在有效期
	}
	if(file_exists($fileName) && $dValue < 3600){
		$content = file_get_contents($fileName);
		echo $content;
		die;
	}else{
		if($re){
			unlink($fileName);//过去先删除,
		}
		require_once ‘coon.php‘;
		$sql = "select * from name order by id;";
		$result = $link->query($sql);
		$arr = array();
		while($re = $result->fetch(PDO::FETCH_ASSOC)){ 
			$arr[] = $re;
		} 
		//循环输出内容到html文件
		ob_start(); //打开缓冲区  
?>
<!-- 下面是输出的内容 -->
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>循环输出的html内容</title>
</head>
<body>
	<table>
		<thead>
			<tr>
				<td>id</td>
				<td>name</td>
				<td>pwd</td>
			</tr>
		</thead>
		<tbody>
			<?php
				foreach ($arr as $key => $value) {
					echo "<tr>";

					echo "<td>{$value[‘id‘]}</td>";
					echo "<td>{$value[‘name‘]}</td>";
					echo "<td>{$value[‘pwd‘]}</td>";

					echo "</tr>";
				}
			?>
		</tbody>
	</table>
	
</body>
</html>
<?php

$content = ob_get_contents();//得到当前缓冲区的内容
ob_end_clean();//删除当前输出缓
file_put_contents(‘index2.html‘,$content);//写入文件
}
?>

首先判断文件是否存在,如果存在则判断当前时间 - 创建时间 的时间差,判断当前文件是否有效。

3、总结

一.是不需要运行在服务器上,访问的时候,服务器只是简单的返回这个文件给浏览器,并不执行任何操作,内存占用小,访问速度快。

二.安全,任何一种动态网站开发语言都不是绝对的安全的,而静态网页除了服务器被黑外,程序不存在任何漏洞

如何使用ob函数输出静态html文件

标签:打开   head   缓冲   inf   chunk   order   ase   []   http   

原文地址:http://www.cnblogs.com/ImCehnyx/p/7147931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!