Home  >  Article  >  Backend Development  >  Teach you how to use the ob function to output static html files

Teach you how to use the ob function to output static html files

巴扎黑
巴扎黑Original
2017-07-23 10:35:161537browse

How to use the ob function to output static html files

1. Introduction to the ob function

1.1. ob_start - open the output control buffer

bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )
This function will open the output buffer. When output buffering is activated, the script will not output content (except http headers), instead the content to be output is stored in an internal buffer.
Reference for details:
#1.2, ob_get_contents - Return the contents of the output buffer

##string ob_get_contents ( void )Just get the output buffer area, but does not clear it. Reference for details:


##1.3, ob_end_flush — flush out (send out) the contents of the output buffer and close the buffer

##bool ob_end_flush ( void )This function will send the contents of the top-level buffer (if there is content inside) and close the buffer. If you want to further process the contents of the buffer, you must call ob_get_contents() before ob_end_flush(), because the buffer contents are discarded after calling ob_end_flush(). Reference for details:


##1.4, ob_flush - flush out (send) the contents of the output buffer

##void ob_flush ( void )This function will send out the contents of the buffer (if there is content inside if). If you want to further process the contents of the buffer, you must call ob_get_contents() before ob_flush(), because the buffer contents will be discarded after calling ob_flush(). This function will not destroy the output buffer, but functions like ob_end_flush() will destroy the buffer. Reference for details:



##1.5, ob_get_clean - get the contents of the current buffer and delete the current output buffer

#string ob_get_clean ( void )Get the contents of the current buffer and delete the current output buffer. ob_get_clean() essentially executes ob_get_contents() and ob_end_clean() together. Reference for details:


##1.6, ob_get_flush - Flush (send out) the buffer content, return the content in the form of a string, and close the output buffer

#string ob_get_flush ( void )ob_get_flush() flushes (sends out) the contents of the buffer, returns the contents as a string, and closes the output buffer.
Note: This function is similar to ob_end_flush(), except that this function also returns the buffer content in string form.
Reference for details:
##2. How to use the ob() function to create a static page of html

2.1. Simple output html file

#f957cddf6f187f9ba867c87323bac3a9
Output hello to index.html


Find index.html, and output the setting normally content

2.2. Obtain database information and output html file

#9a7cdef89b0aab77226f9b80e53fb065query($sql);
$arr ​​= array();
while($re = $result->fetch(PDO::FETCH_ASSOC)){
$arr[] = $re;
}
//Loop output content to html file
ob_start(); //Open buffer
?>
9de4decead90f42d28bdb350786fefc4
8b05045a5be5764f313ed5b9168a17e6
49099650ebdc5f3125501fa170048923
93f0f5c25f18dab9d176bd4f6de5d30e
1fc2df4564f5324148703df3b6ed50c1
b2386ffb911b14667cb8f0f91ea547a7The html content output by loop6e916e0f7d1e588d4f442bf645aedb2f
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
f5d188ed2c074f8b944552db028f98a1
ae20bdd317918ca68efdc799512a9b39
a34de1251f0d9fe1e645927f19a896e8
b6c5a531a458a2e790c1fd6421739d1cidb90dd5946f0946207856a8a37f441edf
b6c5a531a458a2e790c1fd6421739d1cnameb90dd5946f0946207856a8a37f441edf
b6c5a531a458a2e790c1fd6421739d1cpwdb90dd5946f0946207856a8a37f441edf
fd273fcf5bcad3dfdad3c41bd81ad3e5
5a2859caf3b4d47dd1105050a958b333
92cee25da80fac49f6fb6eec5fd2c22a
d0c001435a40f2d88397a6e322744fea $value) {
echo "a34de1251f0d9fe1e645927f19a896e8";
echo "< ;td>{$value['id']}b90dd5946f0946207856a8a37f441edf";
echo "b6c5a531a458a2e790c1fd6421739d1c{$value['name']}b90dd5946f0946207856a8a37f441edf";
echo "< ;td>{$value['pwd']}b90dd5946f0946207856a8a37f441edf";
echo "fd273fcf5bcad3dfdad3c41bd81ad3e5";
}
?>
ca745a59da05f784b8811374296574e1
f16b1740fad44fb09bfe928bcc527e08
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
0b9f0e3442f76d821df9b55025ad958c
Output the result to index2.html


#There are many Output Control functions. I will introduce these types first

2.3 Optimize the reading method to ensure that the specified file is read correctly

<?php
	$fileName = &#39;index2.html&#39;;
	$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 &#39;coon.php&#39;;
		$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[&#39;id&#39;]}</td>";
					echo "<td>{$value[&#39;name&#39;]}</td>";
					echo "<td>{$value[&#39;pwd&#39;]}</td>";

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

$content = ob_get_contents();//得到当前缓冲区的内容ob_end_clean();//删除当前输出缓file_put_contents(&#39;index2.html&#39;,$content);//写入文件}?>
First determine whether the file exists, and if it exists, determine the current time - create The time difference is used to determine whether the current file is valid.

3. Summary

1. It does not need to be run on the server. When accessed, the server simply returns the file to the browser and does not perform any operations. The memory usage is small. Access is fast.

2. Security, no dynamic website development language is absolutely safe, and static web pages do not have any loopholes in the program except for the server being hacked

The above is the detailed content of Teach you how to use the ob function to output static html files. 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