PHP生成SITEMAP.xml

2009 - 7 - 13 作者 : 石家庄SEO 分类 :PHP
 <?PHP   
header('Content-type: application/xml; charset="utf-8"'?true);   
 ?>   
 <?PHP   
$website = "https://www.seosjz.com"; /* change this */   
$page_root = "F:\wwwroot\2009sjzseobg\wwwroot"; /* 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";   
$disallow_dir[] = "site";
$disallow_dir[] = "images";
$disallow_dir[] = "index_files";
$disallow_dir[] = "managerfiles";
$disallow_dir[] = "sitemap";

   
 /* 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";
$disallow_file[] = ".htm";
$disallow_file[] = ".php";  
$disallow_file[] = ".xml";
 
 /* 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">   
<?php   
foreach ($a as $file) {   
?>   
<url>   
<loc><?php echo utf8_encode($website.$file); ?></loc>   
<lastmod><?php 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><?php echo utf8_encode($changefreq); ?></changefreq>   
<priority><?php echo utf8_encode($priority); ?></priority>   
</url>   
<?php   
}   
?>   
</urlset> 

4525 人围观 / 2 条评论 ↓快速评论↓

  • while you're somebody continues to just starting to study the practicing the guitar, you need to know you've got work reduced for you personally. trying to practicing the guitar will need a lot of a lot of time and you will need extremely guitar items to train relating to. practice 's zdklfjksdjghf.

    biuzc 2012-11-24 03:05 回复

  • 正好找这个 可以借鉴一下!

    carlbox 2012-07-12 10:36 回复

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18