Skip to content

Instantly share code, notes, and snippets.

@tux255
Created September 4, 2018 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tux255/d33c8293346a6e408b7decc69b322e22 to your computer and use it in GitHub Desktop.
Save tux255/d33c8293346a6e408b7decc69b322e22 to your computer and use it in GitHub Desktop.
sitemap_generator_cached
<?php
header("Content-type: text/xml");
$cache_time = 3600 * 24; // Time in seconds to keep a page cached
$cache_folder = '/'; // Folder to store cached files (no trailing slash)
$cache_filename = $cache_folder.md5($_SERVER['REQUEST_URI']); // Location to lookup or store cached file
//Check to see if this file has already been cached
// If it has get and store the file creation time
$cache_created = (file_exists($cache_filename)) ? filemtime($cache_filename) : 0;
if ((time() - $cache_created) < $cache_time) {
readfile($cache_filename); // The cached copy is still valid, read it into the output buffer
die();
} else {
ob_start();
?>
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
http://www.w3.org/1999/xhtml http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml" >
<?php foreach ($urls as $url): ?>
<url>
<?php
$link = CHtml::encode((preg_match('#^http#i', $url['loc'])
? $url['loc']
: $this->createAbsoluteUrl($url['loc']))); ?>
<loc><?php echo $link ?></loc>
<?php
if ($url['alternate']) :
foreach ($url['alternate'] as $l => $link) :
?>
<xhtml:link rel="alternate" hreflang="<?php echo $l ?>" href="<?php echo $link ?>"/>
<?php
endforeach;
endif;
?>
<changefreq><?php echo CHtml::encode($url['changefreq']); ?></changefreq>
<priority><?php echo CHtml::encode($url['priority']); ?></priority>
</url>
<?php endforeach; ?>
</urlset>
<?php
ob_end_flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment