php自动生成sitemap地图的代码大纲
如何生成sitemap地图呢?本文分享一例php代码,用于自动动态生成最新的`sitemap地图文件,并通知google网站地图的更新,感兴趣的朋友参考下吧。
内容:
php自动生成sitemap地图
例子,:主要生成sitemap的类。
代码:
复制代码 代码示例:
<?php
// sitemap generator class
class Sitemap
{
// constructor receives the list of URLs to include in the sitemap
function Sitemap($items = array())
{
$this->_items = $items;
}
// add a new sitemap item
function addItem($url,
$lastmod = ”,
$changefreq = ”,
$priority = ”,
$additional_fields = array())
{
$this->_items[] = array_merge(array(‘loc’ => $url,
‘lastmod’ => $lastmod,
‘changefreq’ => $changefreq,
‘priority’ => $priority),
$additional_fields);
}
// get Google sitemap
function getGoogle()
{
ob_start();
header(‘Content-type: text/xml’);
echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>’;
echo ‘<urlset xmlns=”″
xmlns:xsi=””
xsi:schemaLocation=”
”>’;
foreach ($this->_items as $i)
{
echo ‘<url>’;
foreach ($i as $index => $_i)
{
if (!$_i) continue;
echo “<$index>” . $this->_escapeXML($_i) . “</$index>”;
}
echo ‘</url>’;
}
echo ‘</urlset>’;
return ob_get_clean();
}
// escape string characters for inclusion in XML structure
function _escapeXML($str)
{
$translation = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($translation as $key => $value)
{
$translation[$key] = ‘&#’ . ord($key) . ‘;’;
}
$translation[chr(38)] = ‘&’;
return preg_replace(“/&(?![A-Za-z]{0,4}w{2,3};|#[0-9]{2,3};)/”,”&” ,
strtr($str, $translation));
}
}
?>
:调用,具体实现sitemap。
复制代码 代码示例:
<?php
// redirect requests to dynamic to their keyword rich versions
require_once ‘/’;
define(‘SITE_DOMAIN’, ‘’);
// create the Sitemap object
$s = new Sitemap();
// add sitemap items
$s->addItem(SITE_DOMAIN);
$s->addItem(SITE_DOMAIN.”/”);
$s->addItem(SITE_DOMAIN.”/”);
…
//连接数据库,生成URL并通过条用$s->addItem()加入到sitemap中。
// output sitemap
if (isset($_GET['target']))
{
// generate Google sitemap
if (($target = $_GET['target']) == ‘google’)
{
echo $s->getGoogle();
}
}
?>
-
PHP文件上传源码分析
文件上传,一般分为俩种方式FTP和HTTP,对于我们的互联网应用来说:FTP上传虽然传输稳定,但是易用性和安全性都是个问题.你总不至于在用户要上传头像的时候告诉用户”请打开FTP客户端,上传文件到中,并以2dk433423l.jpg命名”吧?PHP文件上传源码分析基于HTTP的上传...
-
PHP程序员基本要求和必备技能
PHP程序员有什么必备知识呢?以下是本站小编精心为大家整理的PHP程序员基本要求和必备技能,希望对大家成为程序员有所帮助!更多内容请关注应届毕业生网!一名PHP程序员的基本要求:1、精通html、CSS、JS等网站前端技术。2、掌握PHP编程技术。3、了解Linux及Apache、...
-
php中zend相对路径问题
php下使用zend相对路径的处理方法。如何使用呢?就跟随本站小编一起去了解下吧,想了解更多相关信息请持续关注我们应届毕业生考试网!<?phpdefine('P_S',PATH_SEPARATOR);define('ROOT',"../");set_include_path(ROOT.P_S.'Zend'.P_S_incl...
-
PHP学习10个基础知识总结
PHP学习过程中,必须要掌握基础的知识点,以下是小编为大家搜索整理的PHP学习10个基础知识总结,希望能给大家带来帮助!更多精彩内容请及时关注我们应届毕业生考试网!PHP10个小基础知识总结1,在HTML嵌入PHP脚本有三种办法:以下为引用的内容:<scriptlanguage="PHP">//嵌...