Creating sitemaps with PHP

How to generate sitemaps dynamically using PHP.

3774 views

Edited: 2021-03-07 17:29

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 timestamps 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 indicating when the resource was last modified; it is important that you keep this up-to-date, so whenever an article is updated, you should re-generate the entire sitemap.

While you could also include the prioity and changefreq elements, apparently these are not used by search engines much. The change frequency is also not important when you include a timestamp via the lastmod element.

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.

<?php
$Query = "SELECT * FROM TABLE_NAME ORDER BY PID DESC LIMIT 50000";
 
// Make sure we send output in UTF-8  
header('Content-type: application/xml; charset=UTF-8');

echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

$result = mysql_query($Query);
while($row = mysql_fetch_array($result)) {
  echo '<url>';
  echo '<loc>http://beamtic.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>';
}

echo '</urlset>';

mysql_close($DB_Connection); ?>

The timestamp 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";

// Make sure we send output in UTF-8  
header('Content-type: application/xml; charset=UTF-8');

echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

$result = mysql_query($Query);
while($row = mysql_fetch_array($result)) {
  echo '<url>';
  echo '<loc>http://beamtic.com/'. $row['url'] .'</loc>';
  echo '<lastmod>' . gmdate("Y-m-d", $row['TimeStamp']) .'T'. gmdate("H:m:s", $row['TimeStamp']) . '+01:00</lastmod>';
  echo '</url>';
}

echo '</urlset>';

mysql_close($DB_Connection);

Tell us what you think:

Anvar Freelancer

This one will generate in page

Do you have an off-page Sitemap Generator?

  1. In this Tutorial, it is shown how to redirect all HTTP requests to a index.php file using htaccess or Apache configuration files.
  2. How to create a router in PHP to handle different request types, paths, and request parameters.
  3. Tutorial on how to use proxy servers with cURL and PHP
  4. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.
  5. How to effectively use variables within strings to insert bits of data where needed.

More in: PHP Tutorials