PHP每15分钟自动更新网站地图(减少服务器消耗)-php教程

资源魔 34 0
比来正在弄一个短网址,本人写的代码。锤炼一下本人。正在做html网站舆图这块,想着把一切天生的短衔接都展现进去,不便收录。就写了一个sitemap.php,起初发现,假如当前人流量年夜或许数据过多的话,效劳器累赘就会特地重,如果有10w条数据,每一个人拜访的时分城市从数据库索引这10w条数据,一秒钟有100集体拜访,效劳器基本累赘不外来。而后就萌发了天生html舆图这个设法主意。

因为学艺没有精,可能思绪上有些不合错误的。心愿有更好思绪可以批判斧正!

原理:

需求三个文件:

  • sitemap.html (这个文件为零碎主动天生,sitemap.php的克隆版)

  • sitemap.php (次要页面,决议页面的款式等,齐全=sitemap.html)

  • timeSitemap.php (为更新顺序,天生html页面。可正在监控宝设置监控。)

sitemap.php为页面文件,sitemap.html为sitemap.php的克隆版,监控宝设置按时监控timeSitemap.php文件,完成每一15分钟天生网站舆图,当然,频次是依照监控宝的监控频次来决议,假如舆图天生失败,会前往404,监控宝会报警。sitemap.xml同理

上面同享代码(用应用的mysql查问等类为本人简略封装的数据库类,这里就没有展现了):

sitemap.php

<?php
/*
@   sitemap html版舆图
*/
// 引入数据库操作类
require_once 'c/class.class.php';
// 引入零碎参数
$config = require 'c/config.php';
?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>网站舆图 - <?php echo $config['web_title']; ?></title>
<meta name="keywords" content="<?php echo $config['web_keywords'];?>">
<meta name="description" content="<?php echo $config['web_description']; ?>">
<link href="<?php //echo $config['web_url'];?>/css/bootstrap.min.css" rel='stylesheet' type='text/css'>
<!--
<link href="<?php //echo $config['web_url'];?>/css/style.css" rel='stylesheet' type='text/css'>
<link href="<?php //echo $config['web_url'];?>/css/media.css" rel='stylesheet' type='text/css'>
<script src="<?php //echo $config['web_url'];?>/css/jquery-3.1.1.min.js"></script>
-->
<link type="favicon" rel="shortcut icon" href="<?php //echo $config['web_url'];?>/favicon.ico" />
<link type="favicon" rel="icon" href="<?php //echo $config['web_url'];?>/favicon.ico" />
<style>
.table tr {
text-align: center;
}
a {
display: inline-block;
padding: 10px;
}
</style>
</head>
<body>
<!--先提醒-->
<?php
// <!-- 掏出一切短网址 -->
$cons = new con();
$consSql = "select * from urls order by id desc";
$consQuery = $cons->query($consSql);
// >> 总数目
$consNum = mysql_num_rows($consQuery);
?>
<div class="container">
<!--<table class="table table-striped table-bordered table-hover table-condensed">-->
<hr>
<div style='text-align:center;height:35px;line-height:35px;font-weight:bold;'>
共<?php echo $consNum; ?>条数据</div><div style='text-align:center;'>本页面每一15分钟更新一次
</div>
</hr>
本站链接:<a href="http://bba.fun">bba.fun短网址</a><a href="http://bba.fun/page/api">api接口</a><a href="http://bba.fun/sitemap.html">网站舆图</a>
<br>
天生链接:
<br>
<?php
// >> 显示总数目
echo "";
// >> 开端轮回掏出
while($rows = mysql_fetch_array($consQuery)){
echo "<a href='{$rows['short_url']}' target='_blank' rel='external nofollow'>".$rows['short_url']."</a>";
}
?>
<!--</table>-->
<div style='text-align:center;height:35px;line-height:35px;font-weight:bold;'>2017© <a href="<?php echo $config['web_url'];?>"><?php echo $config['web_title']; ?></a></div><hr>
</div>
</body>
</html>

timeSitemap.php

<?php
/*
@   按时更新网站舆图
*/
// 界说猎取的url
$url = "http://bba.fun/sitemap.php";
// 定网站舆图名字
$name = "sitemap.html";
// 猎取源码
$html = file_get_contents($url);
// 写入html
$write = file_put_contents($name,$html);
if($write){
header("HTTP/1.1 200");
}else {
header("HTTP/1.1 404");
}
?>

以上就是PHP每一15分钟主动更新网站舆图(缩小效劳器耗费)的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学

抱歉,评论功能暂时关闭!