傻瓜式的php+mysql伪静态(真实存在的html页面)

伪静态你们应该知道
例如你的php页面生成的页面为xxx.php?id=1
为了seo和容易被百度等搜索引擎抓取,也为了防止高并发的时候占用服务器资源
咱们应该把xxx.php?id=1页面的连接显示为xxx_1.html等相似格式,反正就是.html为后缀。php

我本身没有深刻研究这个,可是我简单实现了这个。html

1 简单的列表mysql

<?php
//链接数据库
$con = mysql_connect("localhost","root","root");
//选择数据库
mysql_select_db("test", $con);
//查询数据库
$result = mysql_query("SELECT * FROM list");
//遍历输出数据库
while($row = mysql_fetch_array($result))
  {
      $url = $row["url"];
      $id = $row["id"];
      if(empty($url)){
          echo "<a href='p.php?id=$id'/>$row[title]</a><br/>";
      }else{
          echo "<a href='http://localhost/20180417/$url'/>$row[title]</a><br/>";
      }
  }
mysql_close($con);
?>

如图:sql

clipboard.png

此时,全部的标题超连接显示的都是p.php?id=x
重要的是p.php页面数据库

p.php遍历输出的时候
查询了数据库url字段,若是为空,那么就开始进行file_get_contents获取整个页面的html代码,而后把代码写入到一个名为LKY_$id.html的html文件,其中$id是当前页面的id,若是当前页面id=1,那么生成的文件名为LKY_1.html,接着就把这个文件名更新到数据库的url字段服务器

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
//链接数据库
$con = mysql_connect("localhost","root","root");
//选择数据库
mysql_select_db("test", $con);
//得到id
$id =$_GET["id"];
//查询数据库
$result = mysql_query("SELECT * FROM list where id =".$id);
//遍历输出数据库
while($row = mysql_fetch_array($result))
  {
      $url = $row["url"];
      if(empty($url)){
          $get_html = "http://localhost/20180417/get_html.php?id=$id";
          $html_utl = "LKY_$id.html";
        $con_html = file_get_contents($get_html);
        $html = fopen($html_utl, "w");
        fwrite($html, $con_html);
        fclose($html);
        echo $row["zhengwen"];
        mysql_query("UPDATE list SET url = '$html_utl' WHERE id = '$id'");
      }else{
          echo $row["zhengwen"];
      }
  }
mysql_close($con);
?>
</body>
</html>

当用户访问xxx.p.php?id=1的时候,就会进行查询数据库,若是url字段为空,那么就获取整个页面的html,这个获取html页面的来源又用一个get_html.php进行获取并发

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
//链接数据库
$con = mysql_connect("localhost","root","root");
//选择数据库
mysql_select_db("test", $con);
//得到id
$id =$_GET["id"];
//查询数据库
$result = mysql_query("SELECT * FROM list where id =".$id);
//遍历输出数据库
while($row = mysql_fetch_array($result))
  {
      echo $row["zhengwen"];
  }
mysql_close($con);
?>
</body>
</html>

获取到了以后生成html文件并保存在服务器咱们指定的目录,若是访问xxx.p.php?id=1,判断url字段不为空,则直接输出该页数据或者跳转到LKY_1.html高并发

clipboard.png

那么在首页的列表中也作了相应的判断,判断url是否为空,若是为空,那么输出的是p.php?id=1的超连接,不然从数据库取url字段的html文件名,则输出LKY_1.htmlfetch

clipboard.png

要说的是,这是实实在在生成html文件的!搜索引擎

相关文章
相关标签/搜索