微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP每15分钟自动更新网站地图减少服务器消耗

最近在弄一个短网址,自己写的代码。锻炼一下自己。在做html网站地图这块,想着把所有生成的短连接都展示出来,方便收录。就写了一个sitemap.PHP,后来发现,如果以后人流量大或者数据过多的话,服务器负担就会特别重,假如有10w条数据,每个人访问的时候都会从数据库索引这10w条数据,一秒钟有100个人访问,服务器根本负担不过来。然后就萌生了生成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);
}
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐