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

本文标题:PHP每15分钟自动更新网站地图,减少服务器消耗!
原文链接:http://renpengpeng.com/810.html
最近在弄一个短网址,自己写的代码。锻炼一下自己。在做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");
}
?>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Composer Repositories Composer源 Firegento - Magento模块Comp...
    零一间阅读 4,033评论 1 66
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,870评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,806评论 25 708
  • 亮亮看世界阅读 570评论 0 1
  • 一开始留意这本书是因为在微信读书看到静燕给的评价是“前几天开始看蒋雅淇的《没经验,是你最大的优势》,也许你一看书名...
    余七青阅读 658评论 3 4

友情链接更多精彩内容