Sitemap Class

This class allows you to easily create a sitemap that adheres to the standards set forth by Google, Yahoo! and Bing. I used the following resources to create the ultimate Sitemap class:

This class will:

  • Automatically gzip all of your sitemaps (if desired)
  • Create (and update) a sitemap index file
  • Allow you to separate your urls into different files
  • Limit the number of urls in each file to 50,000
  • Properly escape and format all of the required and optional fields
  • Ping the search engines when your sitemap(s) have been updated

I like to create a sitemap (.php) page that only updates the urls I tell it to, and then this class takes care of the rest.

<?php 
 
require_once ('php/classes/Sitemap.php'); 
 
$sitemap = new Sitemap; 
 
if (get('pages')) { 
  $sitemap->page('pages'); 
  $result = db_query ("SELECT url, created FROM pages"); // 20 pages 
  while (list($url, $created) = $result->fetch_row()) { 
    $sitemap->url($url, $created, 'yearly'); 
  } 
} 
   
if (get('posts')) { 
  $sitemap->page('posts'); 
  $result = db_query ("SELECT url, updated FROM posts"); // 70,000 posts 
  while (list($url, $updated) = $result->fetch_row()) { 
    $sitemap->url($url, $updated, 'monthly'); 
  } 
} 
 
$sitemap->close(); 
unset ($sitemap); 
 
function get ($name) { 
  return (isset($_GET['update']) && strpos($_GET['update'], $name) !== false) ? true : false; 
} 
 
?>

If you call the 'sitemap.php?update=pages-posts' then the following files would be created at your BASE_URL:

  • sitemap-index.xml.gz
  • sitemap-pages-1.xml.gz
  • sitemap-posts-1.xml.gz
  • sitemap-posts-2.xml.gz

If you call 'sitemap.php?update=posts', then only the sitemap-posts-1 and 2.xml.gz files will be recreated, the sitemap-index.xml.gz will be updated, and the search engines will be pinged to notify them of any changes.

object Sitemap ( [ bool $compress= true ] )

This is the constructor for the class, and it creates a sitemap object.

$compress If true (the default), then all of your sitemaps will be gzipped. Set to false if you just want the uncompressed xml.
Example
$sitemap = new Sitemap;

page ( string $name )

This will start a new sitemap page of url's to keep them separate, if desired. If you never call this method then every url will be put into sitemap-index-1,2,3,4....xml.gz.

$name Start putting these url's into "sitemap-{$name}-1.xml.gz"
Example
$sitemap->page('posts');

url ( string $url [, string $lastmod [, string $changefreq [, float $priority ]]] )

This method creates the guts of your sitemap(s). The url is required, and all other paramenters are optional.

$url The relative url you would like indexed. eg. 'path/to/page.html'
$lastmod A [strtotime() parsable] date. The last time this url was updated or modified. All dates will be converted to 'Y-m-d' format within the class.
$changefreq How often you expect this url may change. Valid values are 'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', and 'never'
$priority Between 0 and 1. The relative priority that this url has within your site.
Example
$sitemap->url('example/page.php', 'now', 'weekly', .6);

close ( )

Finishes saving your last page of urls, then pings the search engines. If you get lazy and forget (or just don't) call this method, then the class destructor will finish your thoght, but the search engines will not be pinged.

Example
$sitemap->close();  
unset($sitemap);
Click to Download the PHP Sitemap Class

 Subscribe to our feed


comments powered by Disqus
Copyright © 2012 PHP-Ease.com - PHP Made Easy