I was looking at free, online sitemap generators provided on the Google Webmaster Help Center, but they didn't seem like they could be used for general purpose. My script also allows importing directories: it scans a designated directory and picks out all files therein for indexing (currently doesn't support recursive scanning, though). Also, if you don't want the script to be ran everytime the Google bot (or whoever) peeks at your sitemap, you can have the script export to file all the pages you told it to, and you can have a cron job run the script at certain periods to update the sitemap.
For static files it automatically checks to see when it was last modified and renders that time in accordance to the W3C date-time standards, which is what Google accepts, to the lastmod element. The script automatically applies your site's URL to the beginning of all page URLs, so there's no need to, for example, apply http://www.truefusion.trap17.com/ to the beginning of news.html. The script validates well with W3C and Google.
sitemap.jpg 24.63K
11 downloadsThe Class
<?php
#
# [url="http://www.truefusion.trap17.com/"]http://www.truefusion.trap17.com/[/url]
#
# Google Sitemap XML generator
#
class sitemap {
function sitemap($priority=NULL, $changefreq=NULL)
{
/*
* (double) $priority:
* Range: 0.0 - 1.0
*
* (string) $changefreq:
* Possible values:
* always
* hourly
* daily
* weekly
* monthly
* yearly
* never
*/
header("Content-type: application/xml");
$this->defPriority = $priority;
$this->defChangeFreq = $changefreq;
if (!preg_match('`/$`', $_SERVER['DOCUMENT_ROOT'])){
define("DOCROOT", $_SERVER['DOCUMENT_ROOT']."/");
} else {
define("DOCROOT", $_SERVER['DOCUMENT_ROOT']);
}
define("SITEURL", "http://".$_SERVER['SERVER_NAME']."/");
}
function addDynamicPage($pagename=NULL, $categories=NULL, $keys=NULL, $lastmod=NULL, $priority=NULL, $changefreq=NULL)
{
/*
* assuming URL: [url="http://localhost/cat1/cat2/file_name?key1=value&key2=value"]http://localhost/cat1/cat2/file_name?key1=...&key2=value[/url]
*
* addDynamicPage
* (
* "file_name",
* array("cat1", "cat2"),
* array
* (
* "key1" => "value",
* "key2" => "value"
* )
* )
*/
if (is_null($priority)){
$priority = $this->defPriority;
} if (is_null($changefreq)){
$changefreq = $this->defChangeFreq;
}
if (is_array($categories)){
$categories = implode("/", $categories);
}
$this->dynamicPages[] = array
(
"loc" => (!is_null($categories)) ? htmlentities($categories."/".$pagename.$this->implodeKeysWithValues("=", $keys)) : htmlentities($pagename.$this->implodeKeysWithValues("=", $keys)),
"lastmod" => $lastmod,
"priority" => $priority,
"changefreq" => $changefreq
);
}
function addStaticPage($loc, $priority=NULL, $changefreq=NULL)
{
/* Forces "current working directy" to DOCUMENT_ROOT */
$loc = preg_replace('`^\.\.?/`', "", $loc);
if (is_null($priority)){
$priority = $this->defPriority;
} if (is_null($changefreq)){
$changefreq = $this->defChangeFreq;
}
if (file_exists(DOCROOT.$loc))
{
$this->staticPages[] = array
(
"loc" => htmlentities($loc),
"lastmod" => date("Y-m-d",filemtime(DOCROOT.$loc)),
"priority" => $priority,
"changefreq" => $changefreq
);
}
}
function addDirectory($dir, $priority=NULL, $changefreq=NULL)
{
/*
* Constructs an absolute-relative URL
* for each file in the directory.
*/
$dir = preg_replace('`^\.\.?/`', "", $dir);
$dir = preg_replace('`/$`', "", $dir);
foreach (scandir(DOCROOT.$dir) as $value)
{
if ($value != "." && $value != ".." && is_file(DOCROOT.$dir."/".$value)){
$this->addStaticPage($dir."/".$value);
}
}
}
function implodeKeysWithValues($glue, $keys)
{
if (!is_array($keys) && empty($keys)){
return;
}
foreach ($keys as $key => $value)
{
if (isset($return)){
$return .= "&";
}
$return .= $key.$glue.$keys[$key];
}
return "?".$return;
}
function genXMLData()
{
$start = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
$body .= "\t<url>\n";
$body .= "\t\t<loc>".SITEURL."</loc>\n";
$body .= "\t\t<priority>0.9</priority>\n";
$body .= "\t\t<changefreq>weekly</changefreq>\n";
$body .= "\t</url>\n";
if (is_array($this->dynamicPages))
{
foreach ($this->dynamicPages as $key => $value)
{
$body .= "\t<url>\n";
$body .= "\t\t<loc>".SITEURL.$this->dynamicPages[$key]['loc']."</loc>\n";
if (!empty($this->dynamicPages[$key]['lastmod'])){
$body .= "\t\t<lastmod>".$this->dynamicPages[$key]['lastmod']."</lastmod>\n";
}
$body .= "\t\t<priority>".$this->dynamicPages[$key]['priority']."</priority>\n";
$body .= "\t\t<changefreq>".$this->dynamicPages[$key]['changefreq']."</changefreq>\n";
$body .= "\t</url>\n";
}
}
if (is_array($this->staticPages))
{
foreach ($this->staticPages as $key => $value)
{
$body .= "\t<url>\n";
$body .= "\t\t<loc>".SITEURL.$this->staticPages[$key]['loc']."</loc>\n";
if (!empty($this->staticPages[$key]['lastmod'])){
$body .= "\t\t<lastmod>".$this->staticPages[$key]['lastmod']."</lastmod>\n";
}
$body .= "\t\t<priority>".$this->staticPages[$key]['priority']."</priority>\n";
$body .= "\t\t<changefreq>".$this->staticPages[$key]['changefreq']."</changefreq>\n";
$body .= "\t</url>\n";
}
}
$end = "</urlset>\n";
return $start.$body.$end;
}
function renderXMLFile($path){
file_put_contents($path, $this->genXMLData());
}
}
?>
Example Usage
<?php
require "sitemap.class.php";
$sitemap = new sitemap(0.5, "monthly");
$sitemap->addDirectory("news");
$sitemap->addStaticPage("affiliates.html", 0.6, "weekly");
$sitemap->addDynamicPage(NULL, array("index.php", "blog"), array("tag" => "PHP"), NULL, 0.7, "daily");
$sitemap->genXMLData();
?>
The above code should, for example, output the following:<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://yoursite/</loc> <priority>0.9</priority> <changefreq>weekly</changefreq> </url> <url> <loc>http://yoursite/index.php/blog/?tag=PHP</loc> <priority>0.7</priority> <changefreq>daily</changefreq> </url> <url> <loc>http://yoursite/news/somenews1.html</loc> <lastmod>2008-10-16</lastmod> <priority>0.5</priority> <changefreq>monthly</changefreq> </url> <url> <loc>http://yoursite/news/somenews2.html</loc> <lastmod>2008-11-16</lastmod> <priority>0.5</priority> <changefreq>monthly</changefreq> </url> <url> <loc>http://yoursite/affiliates.html</loc> <lastmod>2008-12-10</lastmod> <priority>0.6</priority> <changefreq>weekly</changefreq> </url> </urlset>If you use this class, you may report any problems here.















