Home arrow PHP arrow Page 3 - Using PHP to Generate a Customized Google XML Sitemap

The PHP Server Side Script - PHP

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



As discussed in the "Web Application Design" section of this tutorial, the PHP script should accomplish four unique functions aside from the normal functions of retrieving data from the POST array and doing a minor validation task.






//The web form has been submitted, grab the data from POST as well as remove the white space using trim command


$url =trim($_POST['url']);


//Check if the form submitted contains any data.

//Also check if the security code is correct.

//More information about captcha design here http://www.devshed.com/c/a/PHP/Designing-a-Captcha-System-with-PHP-and-MySQL/


if ((empty($url)) || (!(trim($_POST['captcha'])==$_SESSION['answer'])))



//Feedback to the user that the form does not contain any data


die ('ERROR: Enter figures or correct captcha. <a href="/descriptivestats.php">Click here to proceed with the analysis</a>');






//The data from the $url comes from an array (POST),

//You need to explode the data from the array using PHP explode function

//And then assigned the data to a $data variable 

//This will do the job of actually parsing the URL from the web form


$data = explode("n", $url);


//Display to the web browser the heading sections of the XML syntax using Google XML sitemap standard


echo '<font face="Courier New" size="2">';

echo '<font color="#C0547F"><i>&lt;&#63;xml version&#61;&#34;1.0&#34; encoding&#61;&#34;UTF&#45;8&#34;&#63;&#62;</i></font>';

echo '<br />';

echo '<font color="#7C137F"><b>&lt;urlset</b></font> <b>xmlns</b>&#61;<font color="blue">&#34;http://www.sitemaps.org/schemas/sitemap/

echo '<br /><br />';


//The code below computes the priority of the URLs in sitemap.

//The most important URL (the one entered first by the user in the web form has a priority of 1.)

//The minimum or lowest assigned priority is 0.5 regardless of how many URLs are being processed.

//To compute the decrement value or how much is the priority differences between the highest priority of 1 to the lowest assigned priority of 0.5 are to use the formula:

// (0.5)/ ((Number of URLs)-1)

//In PHP, it needs to use the sizeof function to count the number of URLs in the data variable.


$difference = (-0.5)/((sizeof($data))-1);


//The following below is a WHILE Loop which will do the actual tasks of generating the XML syntax for all the URLs



while (($priority>=0.4) && (list($key,$value) = each($data))) {


echo "<font color='#7C137F'><b>&lt;url</b></font>&gt;";

echo "<br />";

echo "&nbsp;&nbsp;&lt;<font color='#7C137F'><b>loc</b></font>&gt;$value&lt;&#47;<font color='#7C137F'><b>loc</b></font>&gt;";

echo "<br />";

echo "&nbsp;&nbsp;&lt;<font color='#7C137F'><b>priority</b></font>&gt;$roundpriority&lt;&#47;<font color='#7C137F'><b>priority</b></font>&gt;";

echo "<br />";

echo "&lt;&#47;<font color='#7C137F'><b>url</b></font>&gt;";

echo "<br /><br />";


//after each loop, the priority is decreased by a difference value computed earlier, the $difference variable is a negative number.




echo "&lt;&#47;<font color='#7C137F'><b>urlset</b></font>&gt;";





<!--Clears out the session variable-->



$_SESSION = array ();

session_destroy ();





>>> 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: