How to Use the ob function to output static html files,
How to Use ob function to output static html file 1. ob function Introduction 1.1, ob_start-open the Output Control Buffer bool ob_start ([callback $ output_callback [, int $ chunk_size [, bool $ erase])
This function enables the output buffer. When the output buffer is activated, the script does not output content (except the http header). On the contrary, the content to be output is stored in the internal buffer.
Http://php.net/manual/zh/function.ob-start.php for details
1.2. ob_get_contents-return the content of the output buffer string ob_get_contents (void)
Only get the content of the output buffer, but do not clear it.
Http://php.net/manual/zh/function.ob-get-contents.php for details
1.3 ob_end_flush-output (output) the buffer content and disable the buffer bool ob_end_flush (void)
This function sends the content of the top-level buffer (if there is content in it) and closes the buffer. To further process the content in the buffer, you must call ob_get_contents () before ob_end_flush () because the buffer content is discarded after ob_end_flush () is called.
Http://php.net/manual/zh/function.ob-end-flush.php for details
1.4. ob_flush-extract (send) the content in the output buffer void ob_flush (void)
This function sends the buffer content (if there is content in it ). To further process the content in the buffer, you must call ob_get_contents () before ob_flush () because the buffer content will be discarded after ob_flush () is called.
This function will not destroy the output buffer, but the buffer will be destroyed like the ob_end_flush () function.
Http://php.net/manual/zh/function.ob-flush.php for details
1.5. ob_get_clean-get the content of the current buffer and delete the slow string ob_get_clean (void) of the current output)
Obtain the content of the current buffer and delete the current output buffer.
Ob_get_clean () actually executes ob_get_contents () and ob_end_clean () together ().
For details, refer to http://php.net/manual/zh/function.ob-get-clean.php1.6?ob_get_flush-to flush the buffer content, return the content in the string format, and disable the output buffer string ob_get_flush (void)
Ob_get_flush () refreshes (sends) the buffer content, returns the content in string format, and closes the output buffer.
Note: This function is similar to ob_end_flush (). The difference is that this function will return the buffer content in string format.
Http://php.net/manual/zh/function.ob-get-flush.php for details
2. How to Use the ob () function to Create html static pages 2.1 and simply output html files <? Php
Ob_start (); // open the buffer
$ Info = 'Hello world !! ';
Using file1_fopen('index.html ', 'w'); // open the file index.html
Fwrite ($ file, $ info); // write the information to index.html
Fclose ($ file); // close the file index.html
?>
Export helloto index.html
Find index.html and output the set content normally.
2.2 obtain database information and output html files <? 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;
}
// Cyclically output content to html files
Ob_start (); // open the buffer
?>
<! -- The following is the output content -->
<! DOCTYPE html>
<Html lang = "en">
<Head>
<Meta charset = "UTF-8">
<Title> html content output cyclically </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 (); // get the content of the Current Buffer
Ob_end_clean (); // Delete the current output
File_put_contents('index2.html ', $ content); // write an object
?>
Output result to index2.html
There are many Output Control functions. We will introduce these functions first.
2.3 optimized the reading method and determined the correct reading of the specified file
<? Php $ fileName = 'index2.html '; $ re = file_exists ($ fileName); // checks whether the file exists $ dValue = 0; if ($ re) {$ fileTime = filectime ($ fileName); // timestamp $ dValue = time ()-$ fileTime; // obtain the creation time, file Cache generally has a validity period.} if (file_exists ($ fileName) & $ dValue <3600) {$ content = file_get_contents ($ fileName); echo $ content; die ;} 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;} // cyclically output content to the html file ob_start (); // open the buffer?> <! -- The following is the output content --> <! DOCTYPE html>
First, determine whether the file exists. If yes, determine the time difference between the current time and the Creation Time to determine whether the current file is valid.
3. Summary
1. It does not need to run on the server. During access, the server simply returns the file to the browser without performing any operations. The memory usage is small and the access speed is fast.
2. Security: none of the dynamic website development languages is absolutely secure. In addition to being hacked by servers, static Web pages do not have any program vulnerabilities.