PHP sitemap generator script

Posted by & filed under PHP.

Here’s a quick and basic script for generating a sitemap XML file for robots to index your site:

<?php
// $link is an array of URLs that you have pulled from your database or where ever!
$link = array_values(array_unique($link));// get rid of duplicates
 
$dt = date('Y-m-d\TH:i:s') . '+00:00';
$file = '<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      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">
 
';
foreach ($link as $k => $url){
    $priority = ($k < 1) ? "1.0" : "0.8";
    $file .= "<url>
    <loc>$url</loc>
    <priority>$priority</priority>
    <lastmod>$dt</lastmod>
    <changefreq>monthly</changefreq>
</url>
";
}
$file .= "</urlset>
    ";
echo file_put_contents('sitemap.xml', $file);// write to file
?>

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Bitnami