Jump to content
xisto Community
Sign in to follow this  
truefusion

Google Xml Sitemap Generator

Recommended Posts

Rather than manually typing out all the pages on my site with their location, priority, change frequency, etc, i decided to type out a PHP class that would do that for me. My site runs off of a flat-file CMS i made specifically for my site. The URL pattern may possibly change in the future (though i'm liking the current set-up), therefore rather than having to update the XML data manually for any new format, why not have a script do that for me, especially if i'm going to be adding lots more pages? The class supports both dynamic and static pages. It was built for general purposes, therefore if you want it to support your CMS, you'll have to subclass it like i did with mineâthat is, if you want to keep this class as general as possible.

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://forums.xisto.com/no_longer_exists/ to the beginning of news.html. The script validates well with W3C and Google.

post-8528-1229448336_thumb.jpg

The Class


<?php## [url="truefusion.trap17.com/"]truefusion.trap17.com/;## 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=\"sitemaps.org/schemas/sitemap/0.9/;; $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


<?phprequire "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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.