Home arrow PHP arrow Using PHP to Generate a Customized Google XML Sitemap

Using PHP to Generate a Customized Google XML Sitemap

Submitting XML sitemaps to Google Webmaster Tools is an important webmaster activity. Google recommends using it to help it find new content in your website which otherwise cannot be crawled by Googlebot. This is particularly helpful if you have a fairly large website. In this article, you'll learn how to generate such a sitemap with PHP.

  1. Using PHP to Generate a Customized Google XML Sitemap
  2. The Web Form's HTML Code
  3. The PHP Server Side Script
  4. Implementation Guide
By: Codex-M
Rating: starstarstarstarstar / 4
June 16, 2010

print this article



If you are new to XML sitemaps or need more information about Google XML sitemap generation, you should read this tutorial before going any further.

This tutorial helps you develop your own PHP-based Google XML Sitemap generator. This is a customized approach, which means that you can feed the application your canonical URLs (sorted by highest to lowest in terms of importance and priority) and it will output the XML syntax for your sitemap.xml.

The XML syntax can then be copy to a blank text file (for example, in notepad) and uploaded to your website.

This offers a lot of advantages compared to other XML sitemap solutions available online, for two reasons. First, Google advises webmasters to submit XML sitemaps that contain ONLY canonical URLs. If you use third party sitemap generator software that you can find online, you will need to do a lot of work filtering the sitemap's XML code for canonical URLs.

Second, other sitemap generators impose a limit. For example, they may only process 500 URLs. This can strongly affect you if your website has more than 500 canonical URLs.

Web Application Design of Customized Sitemap Generator

Any PHP web application starts with a design.  The canonical URLs are put into a web form. Since there is more than one line of input (the typical number of canonical URLs for a large website can exceed 100), you are going to use a text area instead of a single text box, so that all URLs can be accommodated.

Screen shot:


The actual PHP script will then be used to perform four tasks:

1. Parse the URL from the web form.

2. Compute the priorities of the URL, which is used for the sitemap.

3. Generate the XML syntax.

4. Output the generated syntax with computed priorities back to the user's web browser.

The XML syntax should conform to the Google XML sitemap standard. An example of a conforming XML syntax is:

<?xml version="1.0" encoding="UTF-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">



  <loc>http://www.php-developer.org/ </loc>





  <loc>http://www.php-developer.org/how-long-does-it-take-for-dns-to-propagate-in-godaddy-hosting/ </loc>





  <loc>http://www.php-developer.org/solution-for-blogger-you-are-about-to-redirected-message-problem/ </loc>




To easily troubleshoot XML syntax, it is best to use standard colors for the XML syntax. You can easily distinguish the URLs from the XML tags:

Of course, since you are accepting user inputs, you might as well filter bots from using the web form with a security code or a captcha system.

>>> More PHP Articles          >>> More By Codex-M

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: