I often see people on the Internet to ask how the entire dynamic Web site static, in fact, the implementation of the method is very simple.
The code is as follows |
Copy Code |
<?php Add Ob_start () at the beginning of your service; Ob_start (); Here's your code. Add Ob_end_clean () at the end and output this page to a variable $temp = Ob_get_contents (); Ob_end_clean (); Write to File $fp = fopen (' filename ', ' w '); Fwrite ($FP, $temp) or Die (' Write file error '); ?>
|
This is just the basic method, not very practical, because the site is to be updated, to periodically regenerate the HTML
Here are the methods I used:
The code is as follows |
Copy Code |
if (file_exists ("xxx.html")) { $time = time ();
File modification time and now time difference half an hour, directly to the HTML file, otherwise regenerate HTML if ($time-filemtime ("xxx.html") < 30*60) { Header ("Location:xxx.html"); } } Add Ob_start () at the beginning of your service; Ob_start (); Details of the page Add Ob_end_clean () at the end and output this page to a variable $temp = Ob_get_contents (); Ob_end_clean (); Write to File $fp = fopen (' xxx.html ', ' W '); Fwrite ($FP, $temp) or Die (' Write file error '); Re-guide Header ("Location:xxx.html"); |
The cache file used above is overloaded in a large number of builds, so here's a more efficient way to do this
The following is the submission page for the input:
FileName: aa.html
The code is as follows |
Copy Code |
<title> Submit Page </title> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <body> <form method= "POST" action= "bb.php" > Title: <input type= "text" name= "HTMLTitle" ><br> Content: <textarea rows= "8" cols= "Name=" "HTMLBody" ></textarea><br> <input type= "Submit" name= "Submit" value= "Add News" > </form> </body>
|
The following is a code fragment:
FileName: bb.php
The code is as follows |
Copy Code |
<?php Defining date Functions function GetDateTime () { $datetime =getdate (); $strReturn = $datetime ["Year"]. " -"; $strReturn = $strReturn. $datetime ["Mon"]. " -"; $strReturn = $strReturn. $datetime ["Mday"]; return $strReturn; } Define time function (filename) function gettime () { $times =getdate (); $strtime = $times ["Year"]; $strtime = $strtime. $times ["mon"]; $strtime = $strtime. $times ["Mday"]; $strtime = $strtime. $times ["Minutes"]; $strtime = $strtime. $times ["seconds"]; return $strtime; } ?> <?php Determine if the commit value is empty $submit =$_post["Submit"]; Define File header information $htmltitle =$_post["HTMLTitle"]; Define file Contents $htmlbody =$_post["HTMLBody"]; if ($submit) { Define HTML file tags $html 1= $html 1. " $html 1= $html 1. " $html 1= $html 1. " <title> "; $html 1= $html 1. $htmltitle; $html 1= $html 1. " </title> "; $html 1= $html 1. " <meta http-equiv= ' content-type ' content= ' text/html; charset=gb2312 ' > '; $html 1= $html 1. " $html 1= $html 1. " <body> "; $html 1= $html 1. " <table border= ' 1 ' width= ' 740 ' cellpadding= ' 2 ' cellspacing= ' 0 ' bordercolordark= ' #f7f7f7 ' bordercolorlight= ' CCCCCC ' ><tr><td align= ' center ' bgcolor= ' #f7f7f7 ' height= ' ><font ' 3 ' ><b> '; $html 1= $html 1. $htmltitle; $html 1= $html 1. " </b></font></td></tr> "; $html 1= $html 1. " <tr><td><font size= ' 2 ' > '; $html 1= $html 1. $htmlbody; $html 1= $html 1. " </font></td></tr></table> "; $html 1= $html 1. " </body> "; $html 1= $html 1. "
Determine if today's folder exists if (!is_dir (GetDateTime ())) { If it doesn't exist, create mkdir (GetDateTime (), 0777); } Write HTML file $filedir =getdatetime (); $filename =gettime (); $filename = $filename. ". HTML "; $FP =fopen ("$filedir/$filename", "w"); Fwrite ($fp, $html 1); Fclose ($FP); echo "<script>alert (' file write succeeded '); location.href= ' 111.php ';</script>"; } ?> |
If the prompt file is written successfully, you are successful, and then go back to your directory to see if there are any static HTML files generated!
Smarty Template Generation Method
The code is as follows |
Copy Code |
<?php Require_once ("./config/config.php"); Ob_start (); $id =$_get[id]; $sql = "SELECT * FROM table_name where id= ' $id '"; $result =mysql_query ($sql); $rs =mysql_fetch_object ($result); $smarty->assign ("Showtitle", $rs->title); $smarty->assign ("Showcontent", $rs->content); $smarty->display ("content.html"); $this _my_f= ob_get_contents (); Ob_end_clean (); $filename = "$id. html"; Tohtmlfile_cjjer ($filename, $this _my_f); File generation functions function Tohtmlfile_cjjer ($file _cjjer_name, $file _cjjer_content) { if (Is_file ($file _cjjer_name)) { @unlink ($file _cjjer_name); exists, you delete } $cjjer _handle = fopen ($file _cjjer_name, "w"); Create a file if (!is_writable ($file _cjjer_name)) {//Judge Write permission return false; } if (!fwrite ($cjjer _handle, $file _cjjer_content)) { return false; } Fclose ($cjjer _handle); Close pointer return $file _cjjer_name; Return file name } ?> |
Smarty has a way to get Template page content method Fetch (), its declaration prototype is this:
The code is as follows |
Copy Code |
<?php
function fetch ($resource _name, $cache _id = null, $compile _id = null, $display = False)
?> |
The first parameter is the template name, the second parameter is the cached ID, the third parameter is the compilation ID, and the fourth parameter is whether the template content is displayed. We need to use this method to generate a static page.
The code is as follows |
Copy Code |
<?php $smarty = new Smarty (); Other template substitution syntax ...
The following sentence gets all the contents of the page, noting that the last argument is False $content = $smarty->fetch (' template name. TPL ', NULL, NULL, FALSE);
The following writes the content to a static file $fp = fopen (' news.html ', ' W '); Fwrite ($fp, $content); Fclose ($FP);
OK, here's the news.html static page, and you can take care of your next job. ?> |
All right, well, combined with the above method, we generate files almost the same principle, first read the data out and then give us the definition of a good template, and finally use the fopen function to generate an. html file.