Teach you how to use the OB function to output static HTML files

Source: Internet
Author: User
Tags filetime flushes

How to use the OB function to output a static HTML file

1, OB function introduction

1.1. ob_start-Open Output Control buffer

bool ob_start ([ callback $output _callback [, int $chunk _size [, bool $erase]])
This function opens the output buffer. When the output buffer is activated, the script will not output content (except for the HTTP header), and the output will be stored in the internal buffer instead.
Detailed reference:

1.2. ob_get_contents-returns the contents of the output buffer

String ob_get_contents ( void)
Just get the content of the output buffer, but do not clear it.
Detailed reference:

1.3. ob_end_flush-flushes out (sends out) the output buffer content and closes the buffer

BOOL Ob_end_flush ( void)
This function will send the contents of the topmost 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 ().
Detailed reference:

1.4. ob_flush-flushes out (sends out) 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 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 does not destroy the output buffer, and the like Ob_end_flush () function destroys the buffer.
Detailed reference:

1.5. ob_get_clean-Gets the contents of the current buffer and deletes the current output

String Ob_get_clean ( void)
Gets the contents of the current buffer and deletes the current output buffer.
Ob_get_clean () is essentially executed together with ob_get_contents () and Ob_end_clean ().
Detailed reference:

1.6. ob_get_flush-Brush out (send out) buffer content, return content as a string, and close the output buffer

String Ob_get_flush ( void)
Ob_get_flush () brushes out (sends out) the contents of the buffer, returns the content as a string, and closes the output buffer.
Note: This function is similar to Ob_end_flush (), but the function also returns the buffer contents as a string.
Detailed reference:

2. How to use OB () function to make static page of HTML

2.1. Simple output HTML file

< Span class= "methodname" > <?php
Ob_start ();//Open buffer
$info = ' Hello world!! ';
$file =fopen (' index.html ', ' w ');//Open File index.html
fwrite ($file, $info);//write information to index.html
Fclose ($file); /close file index.html
?
Output Hello to index.html

Find index.html, normal output of the set content

2.2. Get the database information output HTML file

<?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;
}
Loop output content to HTML file
Ob_start (); Open buffer
?>
<!--Here's what the output is--
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> the HTML content of the loop output </title>
<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>
<?php
$content = Ob_get_contents ();//Gets the contents of the current buffer
Ob_end_clean ();//delete current output slow
File_put_contents (' index2.html ', $content);//write file
?>
Output results to index2.html

There are many Output Control functions, which are described in the first

2.3 Optimize the Read mode to determine the correct reading of the specified file

<?php$filename = ' index2.html '; $re = file_exists ($fileName);//Determine if the file exists $dvalue = 0;if ($re) {$fileTime = Filectime ($ FileName);//Timestamp $dvalue = time ()-$fileTime;//Get creation time, file cache is usually valid}if (file_exists ($fileName) && $dValue < 3600) {$content = file_get_contents ($fileName); echo $content;d ie;} Else{if ($re) {unlink ($fileName);//delete in the past,}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;}// Loop output content to HTML file Ob_start (); Open buffer?><!--below is the content of the output--><! DOCTYPE html>

First, determine whether the file exists, if any, determine the current time-the time difference between the creation, to determine whether the current file is valid.

3. Summary

One. is not required to run on the server, when access, the server simply returns this file to the browser, does not perform any operation, the memory consumption is small, access speed is fast.

Two. Security, any kind of dynamic website development language is not absolutely safe, and static Web page in addition to the server is black, the program does not have any loopholes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.