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.

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

 

<url>

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

  <priority>1</priority>

</url>

 

<url>

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

  <priority>0.97</priority>

</url>

 

<url>

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

  <priority>0.95</priority>

</url>

</urlset>

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.

{mospagebreak title=The Web Form’s HTML Code}

Let’s create the easy coding for the web form, which will be used to get URL inputs from the users.

Note: the blue font color is for the code discussion or description.

//Start session for captcha or security code

 

<?php

session_start();

?>

 

<!–Start the head tag as well as display the page title–>

 

<html>

<body>

<head>

<title>PHP XML sitemap code generator</title>

</head>

 

<?php

 

//Check if the form is submitted

 

if (!$_POST['submit'])

{

 

//If the web form is not submitted, display the web form

 

?>

 

<!–This is the actual web form–>

 

<br />

 

<!–This form will submit the data to itself using PHP SELF–>

 

<form action="<?php echo $SERVER['PHP_SELF']; ?>"

method="post">

<font face="Verdana" size="2">

 

<!-Below is the form content, instructions on how to use–>

 

<!–You can enter any content here for your own web application, a sample content is shown in the screenshot below–>

 

<!–The Text area HTML code which will accepts user inputs in terms of URL–>

 

<!–It also includes the captcha or the security code–>

 

<textarea name="url" rows="18" cols="120"></textarea>

<br /><br />

<img src="/xmlsitemapgenerator/antibot.php" />

<br />

Type the anti-bot code above:

<br /> <br />

<input type="text" name="captcha" size="10">

<br /> <br />

<input type="submit" name="submit" value="Generate Sitemap XML code">

</form>

<a href="/xmlsitemapgenerator/xmlsitemapgenerator.php">Click here to reset or clear this form</a> 

The above is the complete web form code. An example of the completed form should look like this:

{mospagebreak title=The PHP Server Side Script}

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.

<?php

}

else

{

 

//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>’);

 

}

else

{

 

//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/
0.9&#34;</font>&gt;';

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

 

$priority=1.0;

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

$roundpriority=round($priority,2);

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.

 

$priority=$priority+$difference;

}

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

}

}

?>

 

<!–Clears out the session variable–>

 

<?php

$_SESSION = array ();

session_destroy ();

?>

</body>

</html>

 

{mospagebreak title=Implementation Guide}  

The SEO Chat article Create a Customized Google XML Sitemap discusses the details for how to implement this web application.

Since this is a customized XML sitemap generator, careful attention must be given to ensure three things. First, you need to make sure that the URLs entered in the web form are arranged from the most important (e.g home page) to least important (e.g privacy policy).

See the screen shot below for a clear illustration.

Second, you need to use entity escape codes for the characters discussed on this page. You need to apply escape codes before pasting those entire URL list into the form.  

Third, you need to simply copy and paste the result on the web application output to a blank text file and save it as sitemap.xml

Finally, using your FTP to upload it to the root directory, you can then submit that sitemap to Google webmaster tools.

Note: You can download the entire sample web application as a zip file. 

A complete working web application can also be found. 

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye