PHP/XML Sitemap Generator

Page last updated on 2011 / 04 / 09

XML Sitemaps are a useful method for search engines to quickly pick up new content they would otherwise have to find via hyperlinks on the web. Sitemaps can also provide useful metadata regarding URLs such as its last modification date, how often the content changes and the relative importance of the URL in comparison to the rest of the site.

The following code allows you to easily maintain an XML sitemap using PHP. You can:

The script is very simple and currently only takes into account a URL and its last modification date, with the latter being optional. This allows you to easily populate the sitemap with existing and new URLs and easily indicate when content has been updated.

If you have a large number of URLs, you will need to tinker the script to accommodate more than one sitemap, and also have a sitemap index file.

Simply save the contents of the two scripts below and try it for yourself. The references to Sitemap.xml assume that you are in your domain's root directory, otherwise, you should ensure your Sitemap.xml is saved in the root directory to avoid complications, unless it is referenced in a sitemap index file.


  1. <?php
  2. // Save as sitemap.php
  3.  
  4. class sitemap
  5. {
  6. public function load()
  7. {
  8. global $dom;
  9. $dom = new DOMDocument;
  10. $dom->preserveWhiteSpace = true;
  11. $dom->loadXML(file_get_contents('Sitemap.xml'));
  12. }
  13.  
  14. public function generate()
  15. {
  16. global $dom,$urlset;
  17. $dom = new DOMDocument();
  18. $dom->loadXML('<?xml version="1.0" encoding="UTF-8"?>
  19. <urlset
  20. xml:ns="http://www.sitemaps.org/schemas/sitemap/0.9"
  21. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22. xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
  23. http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
  24. </urlset>');
  25. $dom->save('Sitemap.xml');
  26. }
  27.  
  28. public function addrow($vars)
  29. {
  30. global $dom,$urlset;
  31. if(!isset($urlset))
  32. $urlset = $dom->getElementsByTagName('urlset')->item(0);
  33. $node = $dom->createElement('url');
  34. $node->setAttribute('id',md5($vars['loc']));
  35. foreach($vars as $key => $var)
  36. {
  37. $node2 = $dom->createElement($key);
  38. $node3 = $dom->createTextNode($var);
  39. $node2->appendChild($node3);
  40. $node->appendChild($node2);
  41. }
  42. $newnode = $urlset->appendChild($node);
  43. $vars['md5'] = md5($vars['loc']);
  44. }
  45.  
  46. public function editrow($id)
  47. {
  48. global $dom;
  49. $xpath = new DOMXPath($dom);
  50. $mod = $xpath->query("/urlset/url[@id='$id']/lastmod");
  51. $mod->item(0)->nodeValue = strftime("%Y-%m-%d",time() - gmmktime() + mktime());
  52. $dom->save('Sitemap.xml');
  53. }
  54.  
  55. public function deleterow($id)
  56. {
  57. global $dom;
  58. $xpath = new DOMXPath($dom);
  59. $urlset = $dom->getElementsByTagName('urlset')->item(0);
  60. $row = $xpath->query("/urlset/url[@id='$id']")->item(0);
  61. if($row)
  62. $row->parentNode->removeChild($row);
  63. $dom->save('Sitemap.xml');
  64. }
  65. }
  66.  
  67. ?>
  68.  
  69. <?php
  70. // Save as test.php
  71.  
  72. // Example URLs
  73. $urls = array(
  74. 'http://www.example.com/',
  75. 'http://www.example.com/page2',
  76. 'http://www.example.com/page3',
  77. 'http://www.example.com/page4',
  78. 'http://www.example.com/page5',
  79. 'http://www.example.com/page6'
  80. );
  81. include_once('sitemap.php');
  82.  
  83. // Action 1. Create a sitemap
  84. $_sitemap = new sitemap;
  85. $_sitemap->generate();
  86.  
  87. // Action 2. Add URLs to the sitemap (e.g. when you add new content)
  88. // Using a fictional "last modified" date for each URL of 2010-01-01, otherwise
  89. // Use strftime("%Y-%m-%d",$unix_timestamp) for actual last modification dates of URLs or Use strftime("%Y-%m-%d",time()) to set the last modification date for today
  90. // (e.g. a file's last modified date or a mySQL column)
  91. $_sitemap = new sitemap;
  92. $_sitemap->load(); // Load up Sitemap.xml
  93. foreach($urls as $url) // Iterate through example URLs and add to sitemap
  94. {
  95. $array = array('loc'=>$url,'lastmod'=>'2010-01-01');
  96. $_sitemap->addrow($array);
  97. }
  98. $dom->save('Sitemap.xml'); // Save sitemap
  99.  
  100.  
  101. // Action 3. Update a last modification date (e.g. when you update existing content)
  102. // Each URL in the sitemap has an MD5 attribute so uniquely reference it
  103. $_sitemap = new sitemap;
  104. $_sitemap->load();
  105. $_sitemap->editrow(md5($urls[1]));
  106.  
  107. // Action 4. Delete a URL from the sitemap (e.g. when a URL is no longer available on your site)
  108. $_sitemap = new sitemap;
  109. $_sitemap->load();
  110. $_sitemap->deleterow(md5($urls[2]));
  111.  
  112. ?>
  113.  

For further reading regarding sitemaps, visit Sitemaps.org.


Previous Article
How to Create Website Thumbnails with PHP
Next Article
A Simple PHP/MySQL Authentication Script for Logins




Tweet