php教程

PHP生成sitemap网站地图代码

php教程 51源码 2022-11-03 人阅读

网站地图生成代码

<?php
$dom = new DOMDocument("1.0", "utf-8");
header("Content-Type: text/xml");
$root = $dom->createElement("urlset");
$dom->appendChild($root);
for ($i = 0; $i <= 998; $i++) {
$track = $dom->createElement("url");
$root->appendChild($track);
$loc = $dom->createElement("loc");
$track->appendChild($loc);
$lastmod = $dom->createElement("lastmod");
$track->appendChild($lastmod);
$changefreq = $dom->createElement("changefreq");
$track->appendChild($changefreq);
$priority = $dom->createElement("priority");
$track->appendChild($priority);
$text = $dom->createTextNode('http://www.baidu.com/' . (mt_rand(1,20)) . ($i+1) . '.html');
$loc->appendChild($text);
$date = date("Y-m-d", strtotime('-' . rand(0,10) . 'day'));
$text = $dom->createTextNode($date);
$lastmod->appendChild($text);
$text = $dom->createTextNode(daily);
$changefreq->appendChild($text);
$text = $dom->createTextNode(0.8);
$priority->appendChild($text);
}
$dom->save("sitemap.xml");
exit;

代码说明

hangefreq:页面内容更新频率。
lastmod:页面最后修改时间
loc:页面永久链接地址
priority:相对于其他页面的优先权

这样就可以在你的网站根目录下生成sitemap.xml文件

版权声明:文章搜集于网络,如有侵权请联系本站,转载请说明出处:https://www.51yma.cn/jiaocheng/php/380.html
文章来源:
标签 sitemap php代码