论坛登陆 用户: 密码:
联系我们
设为首页
加入收藏
业界新闻 网络编程 程序开发 网页图象 聊天通讯 软件应用 网络安全 硬件学堂 教育频道 站长club
首页>>站长club>>google>>文章正文

google sitemap 生成工具


 日期:2005-7-15 9:29:04     来源:易特网络技术   编辑:黑鹰  点击:

php版:

<?php
header('content-type: application/xml; charset="gb2312"',true);
?>
<!--
@author tobias kluge, enarion.net
@version 0.2, 2005-06-05 17:40 pt
@status working
@update aditya naik, so1o@so1o.net
@licence: lgpl

editor:		mildseven@im286.com

-->
<?php
$website = "http://my.xxxxx.com"; /* change this */
$page_root = "/usr/local/psa/home/vhosts/subdomains/my/httpdocs";		/* change this */

/* maybe change this: */
$changefreq = "weekly"; //"always", "hourly", "daily", "weekly", "monthly", "yearly" and "never".
$priority = 0.8;
/* this sets the last modification date of all pages to the current date */
$last_modification = date("y-m-d\th:i:s") . substr(date("o"),0,3) . ":" . substr(date("o"),3);

/* list of allowed directories */
$allow_dir[] = "web";

/* list of disallowed directories */
$disallow_dir[] = "admin";
$disallow_dir[] = "_notes";

/* list of disallowed file types */
$disallow_file[] = ".inc";
$disallow_file[] = ".old";
$disallow_file[] = ".save";
$disallow_file[] = ".txt";
$disallow_file[] = ".js";
$disallow_file[] = "~";
$disallow_file[] = ".lck";
$disallow_file[] = ".zip";
$disallow_file[] = ".zip";
$disallow_file[] = ".csv";
$disallow_file[] = ".csv";
$disallow_file[] = ".css";
$disallow_file[] = ".class";
$disallow_file[] = ".jar";
$disallow_file[] = ".mno";
$disallow_file[] = ".bak";
$disallow_file[] = ".lck";
$disallow_file[] = ".bak";

/* simple compare function: equals */
function ar_contains($key, $array) {
  foreach ($array as $val) {
	if ($key == $val) {
		return true;
	}
  }
  return false;
}

/* better compare function: contains */
function fl_contains($key, $array) {
  foreach ($array as $val) {
	$pos = strpos($key, $val);
	if ($pos === false) continue;
	return true;
  }

  return false;
}

/* this function changes a substring($old_offset) of each array element to $offset */
function changeoffset($array, $old_offset, $offset) {
  $res = array();
  foreach ($array as $val) {
    $res[] = str_replace($old_offset, $offset, $val);
  }
  return $res;
}

/* this walks recursivly through all directories starting at page_root and
   adds all files that fits the filter criterias */
// taken from lasse dalegaard, http://php.net/opendir
function getfiles($directory, $directory_orig = "", $directory_offset="") {
   global $disallow_dir, $disallow_file, $allow_dir;

   if ($directory_orig == "") $directory_orig = $directory;

   if($dir = opendir($directory)) {
       // create an array for all files found
       $tmp = array();

       // add the files
       while($file = readdir($dir)) {
           	// make sure the file exists
           	if($file != "." && $file != ".." && $file[0] != '.' ) {
               // if it's a directiry, list all files within it
			   //echo "point1<br>";
             if(is_dir($directory . "/" . $file)) {
				  //echo "point2<br>";
             	$disallowed_abs = fl_contains($directory."/".$file, $disallow_dir); // handle directories with pathes
				$disallowed = ar_contains($file, $disallow_dir); // handle directories only without pathes
				$allowed_abs = fl_contains($directory."/".$file, $allow_dir);
				$allowed = ar_contains($file, $allow_dir);
				if ($disallowed || $disallowed_abs) continue;
				if ($allowed_abs || $allowed){
                   $tmp2 = changeoffset(getfiles($directory . "/" . $file, $directory_orig, $directory_offset), $directory_orig, $directory_offset);
                   if(is_array($tmp2)) {
                       $tmp = array_merge($tmp, $tmp2);
                   }
				}
         	} else {  // files
				if (fl_contains($file, $disallow_file)) continue;
                	array_push($tmp, str_replace($directory_orig, $directory_offset, $directory."/".$file));
               	}
           	}
       }

       // finish off the function
       closedir($dir);
       return $tmp;
   }
}

$a = getfiles($page_root);


echo '<?xml version="1.0" encoding="utf-8"?>';
?>

<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<?
foreach ($a as $file) {
?>
   <url>
      <loc><? echo utf8_encode($website.$file); ?></loc>
      <lastmod><? echo utf8_encode(date("y-m-d\th:i:s", filectime($page_root.$file)). substr(date("o"),0,3) . ":" . substr(date("o"),3));?></lastmod>
      <changefreq><? echo utf8_encode($changefreq); ?></changefreq>
      <priority><? echo utf8_encode($priority); ?></priority>
   </url>
<?
}
?>
</urlset>
--------------------
参数说明:
$website--你的域名
$page_root--你的站点的绝对路径
$changefreq--更新时间单位 "always"(始终), "hourly"(小时), "daily"(天), "weekly"(星期), "monthly"(月), "yearly" "never"(从不)
$priority--更新频率
$allow_dir--允许列表的目录
$disallow_dir--禁止列表的目录
$disallow_file--禁止列表的文件类型
-------------------------------
asp版:
<%
' sitemap_gen.asp
' a simple script to automatically produce sitemaps for a webserver, in the google sitemap protocol (gsp)
' by francesco passantino
' www.iteam5.net/francesco/sitemap
' v0.2 released 5 june 2005 (listing a directory tree recursively improvement)
'
' bsd 2.0 license,
' http://www.opensource.org/licenses/bsd-license.php
' 收集整理:	重庆森林@im286.com


session("server")="http://www.xxx.com"                '你的域名
vdir = "/blueidea"                       			'制作sitemap的目录,相对目录(相对于根目录而言)


set objfso = createobject("scripting.filesystemobject")
root = server.mappath(vdir)

response.contenttype = "text/xml"
response.write "<?xml version='1.0' encoding='utf-8'?>"
response.write "<urlset xmlns='http://www.google.com/schemas/sitemap/0.84'>"

set objfolder = objfso.getfolder(root)
'response.write getfilelink(objfolder.path,objfolder.datelastmodified)
set colfiles = objfolder.files
for each objfile in colfiles
	response.write getfilelink(objfile.path,objfile.datelastmodified)
next
showsubfolders(objfolder)

response.write "</urlset>"
set fso = nothing


sub showsubfolders(objfolder)
	set colfolders = objfolder.subfolders
	for each objsubfolder in colfolders
		if folderpermission(objsubfolder.path) then
			response.write getfilelink(objsubfolder.path,objsubfolder.datelastmodified)
			set colfiles = objsubfolder.files
			for each objfile in colfiles
				response.write getfilelink(objfile.path,objfile.datelastmodified)
			next
			showsubfolders(objsubfolder)
		end if
	next
end sub


function getfilelink(file,datafile)
	file=replace(file,root,"")
	file=replace(file,"\","/")
	if fileextensionisbad(file) then exit function
	if month(datafile)<10 then filedatem="0"
	if day(datafile)<10 then filedated="0"
	filedate=year(datafile)&"-"&filedatem&month(datafile)&"-"&filedated&day(datafile)
	getfilelink = "<url><loc>"&server.htmlencode(session("server")&vdir&file)&"</loc><lastmod>"&filedate&"</lastmod><changefreq>daily</changefreq><priority>1.0</priority></url>"
	response.flush
end function


function folderpermission(pathname)

	'需要过滤的目录(不列在sitemap里面)
	pathexclusion=array("\temp","\_vti_cnf","_vti_pvt","_vti_log","cgi-bin")
	folderpermission =true
	for each pathexcluded in pathexclusion
		if instr(ucase(pathname),ucase(pathexcluded))>0 then
			folderpermission = false
			exit for
		end if
	next
end function


function fileextensionisbad(sfilename)
	dim sfileextension, bfileextensionisvalid, sfileext
	'modify for your file extension (http://www.googleguide.com/file_type.html)
	extensions = array("png","gif","jpg","jpeg","zip","pdf","ps","html","htm","asp","php","wk1","wk2","wk3","wk4","wk5","wki","wks","wku","lwp","mw","xls","ppt","doc","swf","wks","wps","wdb","wri","rtf","ans","txt")
'设置列表的文件名,扩展名不在其中的话sitemap则不会收录该扩展名的文件

	if len(trim(sfilename)) = 0 then
		fileextensionisbad = true
		exit function
	end if

	sfileextension = right(sfilename, len(sfilename) - instrrev(sfilename, "."))
	bfileextensionisvalid = false	'assume extension is bad
	for each sfileext in extensions
		if ucase(sfileext) = ucase(sfileextension) then
			bfileextensionisvalid = true
			exit for
		end if
	next
	fileextensionisbad = not bfileextensionisvalid
end function
%>
注意哦,一次指定太多目录的话可能会造成浏览器假死,服务器资源飚升,还有,复制xml内容的时候不要把作者的注释一起复制进去了,哈哈。
上一篇:如何运用搜索引擎加注服务
下一篇:google、雅虎和msn列6月全球搜索市场前三位
[发送给好友] [打印本页] [关闭窗口] [返回顶部转载请注明来源:http://www.iyit.net
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
责任编辑: 黑鹰 投稿作者: 易特网络技术
信息来源: 易特网络技术 录入时间: 2005-7-15 9:29:04
浏览次数: 投稿信箱: shtghy@163.com
设置首页 - 版权声明 - 广告服务 - 关于我们 - 联系我们 - 友情连接
copyrights ©2004-2005 iyit.net all rights reserved. 网站合作、广告联系qq:147007642、466949678
易特网络技术 点击这里给我发消息