百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>网页制作> PHP每15分钟主动更新网站地图(减少办事器耗损)
分享文章到:

PHP每15分钟主动更新网站地图(减少办事器耗损)

发布时间:09/01 来源:未知 浏览: 关键词:
比来在弄一个短网址,本人写的代码。熬炼一下本人。在做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分钟主动更新网站地图(减少效劳器耗损)的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有152人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板