## PHP Sitemap XML Generator
## Version 1.00
## © 2008 Jacob Gifford Head
## http://www.jacob-head.com/
## This work is licenced under the "Creative Commons
## Attribution-Non-Commercial-Share Alike 2.0
## UK: England & Wales Licence"
## For full details see:
## http://creativecommons.org/licenses/by-nc-sa/2.0/uk/
## Comments, suggestions, problems and additions are always welcome.
## phpsitemap ~~ at ~~ jacob-head ~~ dot ~~ com
## Basic Usage:
## 1. Create a file called sitemap.xml [*] in the root directory of your website.
## 2. Ensure that the file is writable by your webserver.
## 3. Edit it so that it reads:
##
##
##
## 4. Save this file as sitemap.inc in the root directory of your website.
## 5. Ensure that it is run when every page is loaded (e.g. by putting at the start of each page.
## [*] The name of this file can be changed below.
## User-editable variables:
$xmlfile = "/sitemap.xml"; ## Path to your sitemap file.
$rooturl = "http://www.mysite.com"; ## Root URL of your site (omit final /)
$includeget = FALSE; ## Include pages which have a GET request in their URL (default FALSE)
## End User-editable variables.
## Begin script itself
## Load the Sitemap XML
$xml = simplexml_load_file($xmlfile);
## To avoid rewriting the XML every time a page is loaded,
## we check to see if writing is necessary (i.e. is this a
## new page or is this an old page which has a new title?)
$writefile=TRUE;
## Do not change if lastmod date has not changed.
foreach ($xml->url as $item){
if ((string) $item->loc == $rooturl.$_SERVER['REQUEST_URI']) {
if ((string) $item->lastmod == date('c', getlastmod($_SERVER['REQUEST_URI']))) {
$writefile=FALSE;
}
}
}
## Do not save if inclues a GET call
if($includeget===FALSE){
$urlincludesget = strpos($_SERVER['REQUEST_URI'], "?");
if ($urlincludesget !== false) {
$writefile=FALSE;
}
}
## If we need to write the file, then…
if ($writefile === TRUE){
##Markup the new entry in XML
$newentry = '
http://www.flageolets.com'.$_SERVER['REQUEST_URI'].'
'.date('c', getlastmod($_SERVER['REQUEST_URI'])).'
';
## Markup old entries in XML, excluding the one
## which we are updating (if relevant).
$oldentries=NULL;
foreach ($xml->url as $item){
if ((string) $item->loc != $rooturl.$_SERVER['REQUEST_URI']){
if ((string) $item->lastmod != date('c', getlastmod($_SERVER['REQUEST_URI']))) {
$oldentries=$oldentries.'
'.$item->loc.'
'.$item->lastmod.'
';
}
}
}
## Open the file for writing
$fileName = fopen ($xmlfile, 'w');
## Into the file, insert the XML Boilerplate followed by the new and then the old entries
fputs ($fileName, '
'.$newentry.chr(13).chr(10).$oldentries.
'');
fclose ($fileName);
}