生产Google Sitemap XML文件的PHP代码
来源:互联网 发布:淘宝2017秋款裙子 编辑:程序博客网 时间:2024/03/29 04:49
php如何生成Google Sitemap XML文件:
参数说明:
$website--你的域名
$page_root--你的站点的绝对路径
$changefreq--更新时间单位 "always"(始终), "hourly"(小时), "daily"(天), "weekly"(星期), "monthly"(月), "yearly" "never"(从不)
$priority--更新频率
$allow_dir--允许列表的目录
$disallow_dir--禁止列表的目录
$disallow_file--禁止列表的文件类型
制作过程说明:
<?php header('Content-type: application/xml; charset="GB2312"',true); $timezone = "Asia/Shanghai";date_default_timezone_set($timezone);$website = "http://www.yipiantian.cn"; /* change this */ $page_root = "/usr/local/psa/home/vhosts/subdomains/my/httpdocs"; /* change this */ /* maybe change this: */ $changefreq = "daily"; //"always", "hourly", "daily", "weekly", "monthly", "yearly" and "never". $priority = 0.5; /* this sets the last modification date of all pages to the current date */ $last_modification = date("Y-m-d H:i:s"); /* list of allowed directories */ $allow_dir[] = "about"; $allow_dir[] = "help"; $allow_dir[] = "case"; $allow_dir[] = "E-Marketing"; /* list of disallowed directories */ $disallow_dir[] = "system"; $disallow_dir[] = "ads"; $disallow_dir[] = "api"; $disallow_dir[] = "ask"; $disallow_dir[] = "category"; $disallow_dir[] = "comment"; $disallow_dir[] = "corpandresize"; $disallow_dir[] = "css"; /* list of disallowed file types */ $disallow_file[] = ".inc"; $disallow_file[] = ".old"; $disallow_file[] = ".save"; $disallow_file[] = ".txt"; $disallow_file[] = ".xml"; $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[] = ".php"; $disallow_file[] = ".sql"; /* 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"));?></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--禁止列表的文件类型
制作过程说明:
将以上代码复制到本地存为sitemap.asp或sitemap.php,上传到你的服务器网站的根目录下,运行一下就可以在你指定的目录中(建议最好是指定为根目录)自动生成一个sitemap.xml文件了。最后进入google提交页面:https://www.google.com/webmasters/sitemaps/login,有gmail的可以用gmail直接登录。登录后把生成的xml文件地址按步骤提交就可以了。注:你的网站如果增加了新的页面,可以在增加完后再运行一下sitemap.asp或sitemap.php,并再重新提交一下sitemap.xml文件,这样就可以保证sitemap.xml文件的不断更新了!
=================================================================================================
<?PHP header('Content-type: application/xml; charset="utf-8"'?true); ?> <?PHP $website = "http://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>
- 生产Google Sitemap XML文件的PHP代码
- PHP生成网站sitemap.xml
- sitemap.xml的写法
- 利用dom4j解析 Google sitemap.xml
- 创建Google网站地图Sitemap.xml
- Google Sitemap-Google的“后门”是什么
- GOOGLE sitemap
- sitemap.xml生成方法(asp和php)
- php生产随机字符的代码
- 用于WordPress的Google Sitemap xml自动生成站点地图插件
- dmp dump文件的代码生产
- sitemap.xml
- Google SiteMap的协议格式详解
- 做了个google sitemap的生成器
- WordPress最简洁google的sitemap
- wordpress代码实现网站地图sitemap的html和xml的方法
- Sitemap:XML和HTML格式的区别
- Django生成网站的sitemap.xml
- javascript中parseInt函数
- handler 文件上传
- 在3ds max中,用opacity map制作真实的叶子
- 第九周实验报告任务4 建立一个二维数组类Douary,使该类中有以下数据成员、成员函数及友员函数,完成矩阵的输入、输出、加、减、相等判断等操作。
- 查找二叉树删除节点的操作
- 生产Google Sitemap XML文件的PHP代码
- 王小波全集
- 第九周任务2
- 第九周任务(三)
- 在MFC的Picture控件上画线和框
- mapgis6.7二次开发vc6_demo 之一(主窗体构架)
- JavaScript 零散笔记
- linux命令总结
- java多线程开发,锁的管理