Home > Article > Backend Development > Complete example of generating html static page with php_PHP tutorial
If you are an SEO worker, you probably want to convert all PHP files into HTML pages. This will be good for website rankings and can also reduce the load on the server Apache. Let me introduce an example of PHP generating a static page. .
addform.php file
The code is as follows | Copy code | ||||
|
The code is as follows | Copy code |
require_once("mysql_inc.php"); //Reference conn.php and connect to the database<🎜>
<🎜>$title=$_POST['title']; $content=$_POST['content']; //Get form variables<🎜> <🎜> //The following creates a text document whose values are automatically counted $countfile="count.txt"; if(!file_exists($countfile)) { fopen($countfile,"w"); //If this file does not exist, it will automatically create one } $fp=fopen($countfile,"r"); $num=fgets($fp,20); $num=$num+1; //The value automatically increases by one each time fclose($fp); $fp=fopen($countfile,"w"); fwrite($fp,$num); //Update its value fclose($fp);<🎜> <🎜> //Use the automatically counted value above to obtain the HTML path $path $houzui=".html"; $path=$num.$houzui; //The path formed in this way will automatically grow, such as 1.html, 2.html, 3.html... Adding a piece of news will automatically add 1<🎜> <🎜>//The following uses SQL statements to add data to the table news $sql="insert into news (id,title,content,path) values ('','".$title."','".$content."','".$path."')"; $query=mysql_query($sql);<🎜> <🎜>//The following is the key point, replace the {title}, {content} tags in the template with the data obtained from the form $fp=fopen("mode.html","r"); //Read-only open template $str=fread($fp,filesize("mode.html"));//Read the content in the template $str=str_replace("{title}",$title,$str); $str=str_replace("{content}",$content,$str);//Replace content fclose($fp);<🎜> <🎜>$handle=fopen($path,"w"); //Open the news path in writing mode fwrite($handle,$str); //Write the content just replaced into the generated HTML file fclose($handle);<🎜> <🎜> //Finishing work: echo "View the news just added"; |
mysql_inc.php数据库连接文件
代码如下
|
复制代码 | ||||