By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Creating sitemaps with PHP

How to generate sitemaps dynamically using PHP.

Edited: 2014-05-27 02:50

To create your sitemap automatically, you should be using a database that the script can connect to. This Article shows how you can easily fetch the pages from the database, and output the correct URLs and last modified time stamps for each URL.

This script uses two important sitemap elements, the loc element, and the lastmod element. The loc element contains the absolute URL to your resource, and the lastmod element contains the date, of when the resource was last modified.

You can easily extend this script to include other sitemap elements, such as the prioity element, or changefreq element, but you would likely only use them if you kept this information saved along with your content in the database.

Generating sitemaps with PHP

Sitemaps can be generated using PHP, which is far easier then updating them manually, or using sitemap generators that will crawl your site.

The first part of the script is the MySQL database connection.

  $MYSQL_Host = 'localhost'; // usually localhost
  $MYSQL_Uname = 'UserName';
  $MYSQL_Pword = 'Password';
  
  $MYSQL_Dbase = 'Name Of Database';
  
  // The MySQL Connection.
  $DB_Connection = mysql_connect($MYSQL_Host, $MYSQL_Uname, $MYSQL_Pword);
  $DB_selected = mysql_select_db($MYSQL_Dbase, $DB_Connection);

Next we have the actual script, which will output the URLs.

  
  $Query = "SELECT * FROM TABLE_NAME ORDER BY PID DESC LIMIT 50000";
  
  header('Content-type: application/xml; charset=UTF-8');
?>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
  $result = mysql_query($Query);
while($row = mysql_fetch_array($result)) {
echo '<url>';
echo '<loc>http://brugbart.com/Articles/'. $row['url'] .'</loc>';
echo '<lastmod>' . gmdate("Y-m-d", $row['TimeStamp']) .'T'. gmdate("H:m:s", $row['TimeStamp']) . '+01:00</lastmod>';
echo '</url>';
}
?>
</urlset>
<?php mysql_close($DB_Connection); ?>

The time stamp which is used in this script, assumes that you used the UNIX format in your database, but it can easily be modified to use other formats.

Full PHP Sitemap Generator Script

The full script used to create the sitemap, should look somewhat similar to the below.

<?php
  $MYSQL_Host = 'localhost'; // usually localhost
  $MYSQL_Uname = 'UserName';
  $MYSQL_Pword = 'Password';
  
  $MYSQL_Dbase = 'Name Of Database';
  
  // The MySQL Connection.
  $DB_Connection = mysql_connect($MYSQL_Host, $MYSQL_Uname, $MYSQL_Pword);
  $DB_selected = mysql_select_db($MYSQL_Dbase, $DB_Connection);

  $Query = "SELECT * FROM TABLE_NAME ORDER BY PID DESC LIMIT 50000";
  
  header('Content-type: application/xml; charset=UTF-8');
?>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
  $result = mysql_query($Query);
while($row = mysql_fetch_array($result)) {
echo '<url>';
echo '<loc>http://brugbart.com/Articles/'. $row['url'] .'</loc>';
echo '<lastmod>' . gmdate("Y-m-d", $row['TimeStamp']) .'T'. gmdate("H:m:s", $row['TimeStamp']) . '+01:00</lastmod>';
echo '</url>';
}
?>
</urlset>
<?php mysql_close($DB_Connection); ?>