Heim > Backend-Entwicklung > PHP-Tutorial > 伪静态html 利用PHP生成静态HTML文档的原理

伪静态html 利用PHP生成静态HTML文档的原理

WBOY
Freigeben: 2016-07-28 08:29:39
Original
1118 Leute haben es durchsucht


利用PHP,将数据库中的文章数据生成单个的HTML文档。首先,有利于搜索引擎的收录。其次,避免数据库中的字段暴露在地址栏上,更安全。 
给出代码:

 利用PHP生成静态HTML文档的原理

<span>php
</span><span>//</span><span>引入数据库配置文件</span><span>include</span>( <span>dirname</span>(<span>dirname</span>(<span>__FILE__</span>))."\include\config.php"<span> );

</span><span>/*</span><span>*
 * 
 * 将数据库中的文章生成单个HTML文件.
 * @param Date $Date
 * @param Time $Time
 * @param String $Content
 * @param String $Title
 </span><span>*/</span><span>function</span> GenerateHTML(<span>$Date</span>,<span>$Time</span>,<span>$Content</span>,<span>$Title</span>,<span>$Name</span><span>){

</span><span>//</span><span>将日期、时间变量分解成数组</span><span>$GetDateRow</span> = <span>explode</span>("-", <span>$Date</span><span>);
</span><span>$GetTimeRow</span> = <span>explode</span>(":",<span>$Time</span><span>);

</span><span>//</span><span>得到文件的名字。比如:20121028210632.html</span><span>$FileName</span> = <span>$GetDateRow</span>[0].<span>$GetDateRow</span>[1].<span>$GetDateRow</span>[2].<span>$GetTimeRow</span>[0].<span>$GetTimeRow</span>[1].<span>$GetTimeRow</span>[2].".html"<span>;

</span><span>//</span><span>打开并读取模板内容</span><span>$FP</span> = <span>fopen</span>("tmp.html","r"<span>);
</span><span>$Str</span> = <span>fread</span>(<span>$FP</span>,<span>filesize</span>("tmp.html"<span>));

</span><span>//</span><span>得到替换后的模板内容</span><span>$Str</span> = <span>str_replace</span>("{Title}",<span>$Title</span>, <span>$Str</span><span>);
</span><span>$Str</span> = <span>str_replace</span>("{Content}", <span>$Content</span>, <span>$Str</span><span>);
</span><span>$Str</span> = <span>str_replace</span>("{Name}", <span>$Name</span>, <span>$Str</span><span>);
</span><span>$Str</span> = <span>str_replace</span>("{Date}", <span>$Date</span>,<span>$Str</span><span>);
</span><span>$Str</span> = <span>str_replace</span>("{Time}", <span>$Time</span>, <span>$Str</span><span>);

</span><span>//</span><span>关闭文件,减少服务器的压力。</span><span>fclose</span>(<span>$FP</span><span>);

</span><span>//</span><span>将内容写入HTML文件</span><span>$Handle</span> = <span>fopen</span>(<span>$FileName</span>,"w"<span>);
</span><span>fwrite</span>(<span>$Handle</span>,<span>$Str</span><span>);
</span><span>fclose</span>(<span>$Handle</span><span>);

</span><span>//</span><span>小测一下
//echo "ok,done!";</span><span>}

</span><span>//</span><span>数据库的操作</span><span>$querysql</span> = "select * from article"<span>;
</span><span>$queryset</span> = <span>mysql_query</span>(<span>$querysql</span><span>);

</span><span>//</span><span>循环生成HTML文件。</span><span>while</span>( <span>$row</span> = <span>mysql_fetch_array</span>(<span>$queryset</span><span>) ){
    GenerateHTML(</span><span>$row</span>['date'],<span>$row</span>['time'],<span>$row</span>['content'],<span>$row</span>['title'],<span>$row</span>['name'<span>]);
}</span>
Nach dem Login kopieren

 利用PHP生成静态HTML文档的原理

OK,DONE!

以上就介绍了伪静态html 利用PHP生成静态HTML文档的原理,包括了伪静态html方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage