【PHP程式】Sitemap.xml 產生器 / 優先權調整 / 製作

終於找到PHP的 Sitemap 產生器了,經過 iZO 精心調整後,傻瓜化了許多。
只要注意調整註解的部分即可。
如果是自己寫網站的朋友們,可以直接使用這個傻瓜sitemap.php,即可快速製作自己的Sitemap.xml唷 !

sitemap.php

<?php
$url = "https://izo.tw/";				//Site,後面請加斜線
$root_dir = 'web/public_html';	//目錄位置
$file_mask = '*.php';						//類型 *.html 或 *
$sitemap_file = 'sitemap.xml'; 				//生成檔案
$always_ignore = array('header.php','admin.php');	//忽略所有目錄的
//忽略檔案
$ignore_files = array(
	'check.php','config.php','function.php','headerN.php','headerW.php','logout.php','sitemap.php','test.php','update.php','upuser.php'
);
//忽略目錄
$ignore_folders = array(
	'admin','pic'
);
$starting_priority = $_POST['starting_priority'];
function generateSiteMap() {
	global $url, $root_dir, $file_mask, $sitemap_file, $starting_priority;
	global $always_ignore, $ignore_files, $ignore_folders;
	global $total_file_count,$average, $lowest_priority_page, $lowest_priority;
	chdir($root_dir);
	$all_pages = getFiles('');
	$xml_string = '<?xml version="1.0" encoding="UTF-8"?>
<urlset
  xmlns="http://www.google.com/schemas/sitemap/0.84"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
                      http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
<!-- created with iZO.tw -->
';
	$modified_priority = array();
	for ($i=30;$i>0;$i--) array_push($modified_priority,$i);
	$lowest_priority = 100;
	$lowest_priority_page = "";
	foreach ($all_pages as $link) {
		$handle = fopen($link,'r');
		$info = fstat($handle);
		fclose($handle);
		$modified_at = date('Y-m-d\Th:i:s\Z',$info['mtime']);
		$priority = $starting_priority;
		if(preg_match('/index\.\w{3,4}$/',$link)) {
			$link = preg_replace('/index\.\w{3,4}$/',"",$link);
			$priority += 20;
		}
		//優先權管理
		if(strpos($link,'view')) $priority -= 15;
		if(strpos($link,'thing')) $priority -= 20;
		if(strpos($link,'other')) $priority -= 25;
		if(strpos($link,'other') !== false) $priority -= 25;
		//資料夾深度
		$depth = substr_count($link,'/');
		if($depth < 1) $priority += 50;
		if($depth > 2) $priority -= $depth * 1;
		if($priority > 100) $priority = 100;
		$loc = $url . $link;
		if(substr($loc,-1,1) == '/') $loc = substr($loc,0,-1);
		$total_priority += $priority;
		if($lowest_priority > $priority) {
			$lowest_priority = $priority;
			$lowest_priority_page = $loc;
		}
		$priority = $priority / 100;
		$xml_string .= " <url>
  <loc>$loc</loc>
  <priority>$priority</priority>
 </url>\n";
	}
		$xml_string .= "</urlset>";
	//	<lastmod>$modified_at</lastmod> 時間標記
	if(!$hndl = fopen($sitemap_file,'w')) {
		print "不能開啟 sitemap file - '$sitemap_file'.\nDumping result to screen...\n<br /><br /><br />\n\n\n";
		print '<textarea rows="25" cols="70" style="width:100%">'.$xml_string.'</textarea>';
	} else {
		print '<p><strong>Sitemap: <a href="' . $url.$sitemap_file .'">'. $url.$sitemap_file .'</a></strong></p>';
		fputs($hndl,$xml_string);
		fclose($hndl);
	}
	$total_file_count = count($all_pages);
}
function getFiles($cd) {
	$links = array();
	$directory = ($cd) ? $cd . '/' : '';
	$files = glob($directory . $GLOBALS['file_mask']);
	foreach($files as $link) {
		if(in_array($link,$GLOBALS['ignore_files'])) continue;
		if(in_array(basename($link),$GLOBALS['always_ignore'])) continue;
		array_push($links, $link);
	}
	$folders = glob($directory . '*',GLOB_ONLYDIR);
	foreach($folders as $dir) {
		$name = basename($dir);
		if(in_array($name,$GLOBALS['always_ignore'])) continue;
		if(in_array($dir,$GLOBALS['ignore_folders'])) continue;
		$more_pages = getFiles($dir);
		if(count($more_pages)) $links = array_merge($links,$more_pages);
	}
	return $links;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sitemap - auto make with iZO.TW</title>
<style type="text/css">
a{color:blue;text-decoration:none;}
a:hover{color:red;}
</style>
</head>
<body>
<?php
if($_POST['action'] == 'Create Sitemap') {
	generateSiteMap();
	echo "<p>找到".$total_file_count."頁面 </p> <a href='sitemap.php'>[返回]</a>";
} else { ?>
<h2>開始建立</h2>
<form action="" method="post">
Priority : <input type="text" name="starting_priority" size="3" value="70" />
<input type="submit" name="action" value="Create Sitemap" />
</form>
<?php } ?>
</body>
</html>