- PHP生成SiteMap文件代碼的方法 推薦度:
- php sitemap文件生成器代碼 推薦度:
- 相關(guān)推薦
PHP生成SiteMap文件的代碼
分享一例php生成sitemap地圖文件的代碼,用于生成google sitemap地圖,有需要的朋友參考學(xué)習(xí)下。
生成sitemap文件
例子:
復(fù)制代碼 代碼示例:
<?php
/**
* 生成sitemap文件
* 平常在用的一段代碼,分享給大家
* edit: www.jbxue.com
*/
require_once('conn.php');
$doc = new DOMDocument('1.0', 'utf-8'); // 聲明版本和編碼
$doc -> formatOutput = true; //格式XML輸出
$sql="select mid from dede_member_company";
$query=mysql_query($sql);
$count=mysql_num_rows($query);
$pg=intval($count/3000);
if($count%3000)
$pg++;
$page=isset($_GET['page'])?$_GET['page']:1;
$getpageinfo=page($page,$count,3000);
$sql="SELECT `mid`,`uptime`,`html_path` FROM `dede_member_company` order by `mid` $getpageinfo[sqllimit]";
$query=mysql_query($sql);
$urlset = $doc -> createElement('urlset');
$xmlns = $doc -> createAttribute('xmlns');
$xmlnsvalue = $doc -> createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9");
$xmlnsxsi = $doc -> createAttribute('xmlns:xsi');
$xmlnsxsivalue = $doc -> createTextNode("http://www.w3.org/2001/XMLSchema-instance");
$schemaLocation = $doc -> createAttribute('xsi:schemaLocation');
$schemaLocationvalue = $doc -> createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
$xmlns -> appendChild($xmlnsvalue);
$xmlnsxsi -> appendChild($xmlnsxsivalue);
$schemaLocation -> appendChild($schemaLocationvalue);
$urlset -> appendChild($xmlns);
$urlset -> appendChild($schemaLocation);
$urlset -> appendChild($xmlnsxsi);
while($row=mysql_fetch_array($query)){
$url = $doc -> createElement('url'); //創(chuàng)建一個標(biāo)簽
$loc = $doc -> createElement('loc'); //創(chuàng)建一個標(biāo)簽
$id = $doc -> createAttribute('id'); //創(chuàng)建一個屬性
$newsid = $doc -> createTextNode($row[mid]); //設(shè)置屬性內(nèi)容
$newsco = $doc -> createTextNode($row[html_path]); //設(shè)置標(biāo)簽內(nèi)容
$lastmod = $doc -> createElement('lastmod');
$modtime = $doc -> createTextNode(date('c',$row[uptime]));
$changefreq = $doc -> createElement('changefreq');
$freqtype = $doc -> createTextNode('daily');
$priority = $doc -> createElement('priority');
$prival = $doc -> createTextNode('0.5');
$id -> appendChild($newsid); //繼承屬性
$loc -> appendChild($id); //繼承屬性內(nèi)容
$loc -> appendChild($newsco); //繼承標(biāo)簽內(nèi)容 //繼承子類
$lastmod ->appendChild($modtime);
$changefreq ->appendChild($freqtype);
$priority ->appendChild($prival);
$url -> appendChild($loc);
$url -> appendChild($lastmod);
$url -> appendChild($changefreq);
$url -> appendChild($priority);
$urlset -> appendChild($url);
}
$doc -> appendChild($urlset);
$doc -> save("../map/sitemap_".$page.".xml");
//=================================
if($page=="1"){
$main_sitemap = new DOMDocument('1.0', 'utf-8'); // 聲明版本和編碼
$main_sitemap -> formatOutput = true; //格式XML輸出
$sitemapindex = $main_sitemap -> createElement('sitemapindex');
$xmlns = $main_sitemap -> createAttribute('xmlns');
$xmlnsvalue = $main_sitemap -> createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9");
$sitemap = $main_sitemap -> createElement('sitemap'); //創(chuàng)建一個標(biāo)簽
$loc = $main_sitemap -> createElement('loc'); //創(chuàng)建一個標(biāo)簽
$id = $main_sitemap -> createAttribute('id'); //創(chuàng)建一個屬性
$newsid = $main_sitemap -> createTextNode($page); //設(shè)置屬性內(nèi)容
$newsco = $main_sitemap -> createTextNode("http://youdomain/map/sitemap_".$page.".xml"); //設(shè)置標(biāo)簽內(nèi)容
$xmlns -> appendChild($xmlnsvalue);
$sitemapindex -> appendChild($xmlns);
$id -> appendChild($newsid); //繼承屬性
$loc -> appendChild($id); //繼承屬性內(nèi)容
$loc -> appendChild($newsco); //繼承標(biāo)簽內(nèi)容
$sitemap -> appendChild($loc); //繼承子類
$sitemapindex -> appendChild($sitemap);
$main_sitemap -> appendChild($sitemapindex);
$main_sitemap -> save("../sitemap.xml");
}else{
$main_sitemap = new DOMDocument('1.0', 'utf-8'); // 聲明版本和編碼
$main_sitemap->preserveWhiteSpace = false;
$main_sitemap -> formatOutput = true; //格式XML輸出
$main_sitemap->load("../sitemap.xml");
$notes = $main_sitemap->documentElement;
$sitemap = $main_sitemap -> createElement('sitemap'); //創(chuàng)建一個標(biāo)簽
$loc = $main_sitemap -> createElement('loc'); //創(chuàng)建一個標(biāo)簽
$id = $main_sitemap -> createAttribute('id'); //創(chuàng)建一個屬性
$newsid = $main_sitemap -> createTextNode($page); //設(shè)置屬性內(nèi)容
$newsco = $main_sitemap -> createTextNode("http://youdomain/map/sitemap_".$page.".xml"); //設(shè)置標(biāo)簽內(nèi)容
$id -> appendChild($newsid); //繼承屬性
$loc -> appendChild($id); //繼承屬性內(nèi)容
$loc -> appendChild($newsco); //繼承標(biāo)簽內(nèi)容
$sitemap -> appendChild($loc); //繼承子類
$notes -> appendChild($sitemap);
$main_sitemap -> appendChild($notes);
$main_sitemap -> save("../sitemap.xml");
}
//
if($page<$pg){
echo "sitemap_".$page."生成完成,正進(jìn)行下一個生成";
$page++;
echo "<script>location.href='?page=".$page."';</script>";
}else{
echo "生成完成!";
exit();
}
function page($page,$total,$pagesize=10,$pagelen=7){
$url=$_SERVER["REQUEST_URI"];
$parse_url=parse_url($url);
$url_path=$parse_url[path];
$url_query=$parse_url[query];
$page=$_GET[page];
if($url_query){
$url_query=ereg_replace("(^|&)page=$page","",$url_query);
$url=str_replace($parse_url["query"],$url_query,$url);
if($url_query) $url.="&page"; else $url.="page";
}else {
$url.="?page";
}
$phpfile=$url;
$pagecode = '';//定義變量,存放分頁生成的HTML
$page = intval($page);//避免非數(shù)字頁碼
$total = intval($total);//保證總記錄數(shù)值類型正確
if(!$total) return array();//總記錄數(shù)為零返回空數(shù)組
$pages = ceil($total/$pagesize);//計算總分頁
//處理頁碼合法性
if($page<1) $page = 1;
if($page>$pages) $page = $pages;
//計算查詢偏移量
$offset = $pagesize*($page-1);
//頁碼范圍計算
$init = 1;//起始頁碼數(shù)
$max = $pages;//結(jié)束頁碼數(shù)
$pagelen = ($pagelen%2)?$pagelen:$pagelen+1;//頁碼個數(shù)
$pageoffset = ($pagelen-1)/2;//頁碼個數(shù)左右偏移量
//生成html
$pagecode='<p class="page">';
$pagecode.="<span>$page/$pages</span>";//第幾頁,共幾頁
//如果是第一頁,則不顯示第一頁和上一頁的連接
if($page!=1){
$pagecode.="<a href=\"{$phpfile}=1\"><<</a>";//第一頁
$pagecode.="<a href=\"{$phpfile}=".($page-1)."\"><</a>";//上一頁
}
//分頁數(shù)大于頁碼個數(shù)時可以偏移
if($pages>$pagelen){
//如果當(dāng)前頁小于等于左偏移
if($page<=$pageoffset){
$init=1;
$max = $pagelen;
}else{//如果當(dāng)前頁大于左偏移
//如果當(dāng)前頁碼右偏移超出最大分頁數(shù)
if($page+$pageoffset>=$pages+1){
$init = $pages-$pagelen+1;
}else{
//左右偏移都存在時的計算
$init = $page-$pageoffset;
$max = $page+$pageoffset;
}
}
}
//生成html
for($i=$init;$i<=$max;$i++){
if($i==$page){
$pagecode.='<span>'.$i.'</span>';
} else {
$pagecode.="<a href=\"{$phpfile}={$i}\">$i</a>";
}
}
if($page!=$pages){
$pagecode.="<a href=\"{$phpfile}=".($page+1)."\">></a>";//下一頁
$pagecode.="<a href=\"{$phpfile}={$pages}\">>></a>";//最后一頁
}
$pagecode.='</p>';
return array('pagecode'=>$pagecode,'sqllimit'=>' limit '.$offset.','.$pagesize);
}
?>
【PHP生成SiteMap文件的代碼】相關(guān)文章:
PHP生成SiteMap文件代碼的方法10-30
php sitemap文件生成器代碼07-11
php自動生成sitemap地圖代碼10-21
php自動生成sitemap地圖的代碼10-07
php自動生成sitemap地圖的代碼07-31