技巧共享
帖子数 4
Google SiteMap
<?
// ZBXE 安装路径
$zbxe_Path = "./"; //手动修改
$loc_base = "/"; //相对路径如http://www.domain.com/
$export_entry_limit = 99999;
if(substr($zbxe_Path,-1)=='/') $zbxe_Path = substr($zbxe_Path, 0, strlen($zbxe_Path)-1);
$config_file = sprintf('%s/files/config/db.config.php',$zbxe_Path);
define('__ZBXE__',true);
if(!file_exists($config_file)) return;
include($config_file);
$info->db_type = $db_info->db_type;
$info->db_port = $db_info->db_port;
$info->db_hostname = $db_info->db_hostname;
$info->db_userid = $db_info->db_userid;
$info->db_password = $db_info->db_password;
$info->db_database = $db_info->db_database;
$info->db_table_prefix = $db_info->db_table_prefix;
$export_type = "document_srl";
$today = date("Ymd");
// 显示数
// 时差设置。日本为+9
$timediff = "+09:00";
function getRegdate($format = 'Y.m.d H:i:s') {
return zdate($str, $format);
}
function zdate($str, $format = 'Y-m-d H:i:s', $conversion=true) {
// parameter null return
if(!$str) return;
// 1970年份处理
if((int)substr($str,0,4) < 1970) {
$hour = (int)substr($str,8,2);
$min = (int)substr($str,10,2);
$sec = (int)substr($str,12,2);
$year = (int)substr($str,0,4);
$month = (int)substr($str,4,2);
$day = (int)substr($str,6,2);
return str_replace(
array('Y','m','d','H','h','i','s','a','M', 'F'),
array($year,$month,$day,$hour,$hour/12,$min,$sec,($hour <= 12) ? 'am' : 'pm',getMonthName($month), getMonthName($month,false)),
$format
);
}
// 1970年以后 使用 ztime() 取 unixtime然后用 date处理
return date($format, ztime($str));
}
/**
* @brief YYYYMMDDHHIISS
* @param str YYYYMMDDHHIISS
* @return int
**/
function ztime($str) {
if(!$str) return;
$hour = (int)substr($str,8,2);
$min = (int)substr($str,10,2);
$sec = (int)substr($str,12,2);
$year = (int)substr($str,0,4);
$month = (int)substr($str,4,2);
$day = (int)substr($str,6,2);
if(strlen($str) <= 8) {
$gap = 0;
} else {
$gap = zgap();
}
return mktime($hour, $min, $sec, $month?$month:1, $day?$day:1, $year)+$gap;
}
function zgap() {
$time_zone = $GLOBALS['_time_zone'];
if($time_zone < 0) $to = -1; else $to = 1;
$t_hour = substr($time_zone, 1, 2) * $to;
$t_min = substr($time_zone, 3, 2) * $to;
$server_time_zone = date("O");
if($server_time_zone < 0) $so = -1; else $so = 1;
$c_hour = substr($server_time_zone, 1, 2) * $so;
$c_min = substr($server_time_zone, 3, 2) * $so;
$g_min = $t_min - $c_min;
$g_hour = $t_hour - $c_hour;
$gap = $g_min*60 + $g_hour*60*60;
return $gap;
}
function getMonthName($month, $short = true) {
$short_month = array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$long_month = array('','January','February','March','April','May','June','July','August','September','October','November','December');
return !$short?$long_month[$month]:$short_month[$month];
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
$db = mysql_connect($info->db_hostname,$info->db_userid,$info->db_password ) or die( "数据库连接失败" );
mysql_select_db($info->db_database,$db) or die( "数据库连接出错" );
header('Content-type: application/xml; charset="utf-8"',true);
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
$query = "select max(last_update) from {$info->db_table_prefix}_documents";
//print_r ($query) ;
$lastmod = zdate(mysql_result( mysql_query( $query ),0 )) . $timediff;
?>
<url>
<loc><?=$loc_base?></loc>
<lastmod><?=$lastmod?></lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?
// DB调取所有文章
$query = "select * from {$info->db_table_prefix}_documents WHERE 1 ORDER BY last_update DESC LIMIT $export_entry_limit";
//print_r ($query) ;
$result = mysql_query( $query );
// 生成每个文章的SITEMAP信息
while( $data = mysql_fetch_array( $result ) )
{
// 加密帖...
if( $data['is_secret'] != "N" )
continue;
if( $export_type == "document_srl" )
$loc = $loc_base . $data['document_srl'];
$lastmod = zdate($data['last_update']) . $timediff;
$lastmodday = zdate($data['last_update'],"Ymd");
//echo "LAST MOD DAY<br />";
//print_r($lastmodday);
?>
<url>
<loc><?=$loc?></loc>
<lastmod><?=$lastmod?></lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<?
}
?>
</urlset>
输出 changefreq 的时候有问题,请修改使用。。。
把上述代码制作一个php文件上传到空间上,
然后到https://www.google.com/webmasters/tools/dashboard
按操作进行添加sitemap


