PHP generates HTML pure static Web program code

Source: Internet
Author: User

about how to use PHP to achieve the whole station static processing content, so that PHP generated HTML pure static Web pages, so that access faster.

The code is as follows Copy Code

Add Ob_start () at the beginning;
Ob_start ();

Here's the 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 ("demo.html"))
{
$time = time ();

File modification time and now time difference half an hour, directly to the HTML file, otherwise regenerate HTML
if ($time-filemtime ("demo.html") < 30*60)
{
Header ("Location:demo.html");
}
}
Add Ob_start () at the beginning;
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 (' demo.html ', ' W ');
Fwrite ($FP, $temp) or Die (' Write file error ');

Re-guide
Header ("Location:demo.html");

The above method may not be the best way to generate in batches.

The code is as follows Copy Code

<?php
if ($_get[pg]== ') {
$AA = 1;
}else{
$aa =$_GET[PG];
}
Include ("admin/conn.php");
Require_once ("page.php");
$result =mysql_query ("SELECT * from 2carsell");
$totle =mysql_num_rows ($result);
$pagelist = $_get[t];
if ($_get[t]== ') {
$pagelist = ' 200 ';
}else{
$pagelist =$_get[t];
}
$pager = new Pager ($totle, $pagelist);
$datastat = "Total <b>". $pager->countall. " </b>, each generation <b> ". $pager->countlist." </b>, a total of Generation <b> "$pager->page." </b> times ";/data statistics
$BB = $pager->page;
$pagenav = $pager->backstr. $pager->thestr. $pager->nextstr;
$limitFrom = $pagelist * ($pager->pg-1);
$result =mysql_query ("SELECT * from 2carsell ORDER by ID DESC limit $limitFrom, $pagelist");
?>
<center><div style= "FONT-SIZE:14PX;" ><b> <font color=red><?echo $aa? ></font > Secondary page generation ... ? Echo $datastat?></b></div><br>
?
Print "<center><textarea name=textarea class=textarea style= ' width:520px;height:455px ' >";
while ($datauser =mysql_fetch_array ($result)) {

$IID = $datauser [id];
$html = file_get_contents ("/moban.php?id=". $iid. "");
$sql = "SELECT * from 2carsell where id= $iid";
$data =mysql_fetch_array (mysql_query ($sql));
$path =date ("Ym", $data [putdate]);
$testdir = "html/". $path;
if (file_exists ($testdir)):
Else
mkdir ($testdir, 0777);
echo "Directory". $testdir. " Create success! <br> ";
endif


$filename = "html/$path/sell_$iid.html";

Open $filename using Write mode
if (! $handle = fopen ($filename, ' W ')) {
Print "Cannot open file $filename";
Exit
}

if (is_writable ($filename)) {

Write the $html to the file we opened.
if (!fwrite ($handle, $html)) {
Print "cannot be written to the file $filename";
Exit
}

Print "File $filename update successful! NR ";

Fclose ($handle);

} else {
Print "File $filename not writable";
}
?>
? }?>
</textarea>
<br><br>

<div style= "font-size=12px" ><? echo $datastat. " "?></div><br><br>
?
$AA = $aa +1;
if ($aa > $bb) {
Echo ' <font color=blue> Congratulations, all pages generated! </font> ';
echo "<script>alert (' All document Generation/update completed!") ') </script>;
}else{
echo "<Script> window.location= ' make.php?t= $pagelist &pg= $aa '; </script> ";
}
?>

Note: The original dynamic address is moban.php?id=1, and the post generated address is html/200808/sell_1.html. Page.php is a paging program, published in this blog.
How the page is used, save this code as make.php, and use the method to access the make.php?t= number &pg= pages for the browser; For example, make.php?t=300&pg=2, which generates 300 data at a time, starts at page 2nd of the list of data. Skip the front 300. If you access make.php directly without any arguments, 200 is generated by default each time, starting at the first page.

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.