Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Help With Creating An Auto Gen Sitemap

12:26 am on Jun 12, 2009 (gmt 0)

Full Member

10+ Year Member

joined:June 16, 2004
votes: 0

Hi, I am trying to create an automated Sitemap that will be run through a cronjob, however am having trouble with php on how to use fopen to read a file and edit it from a specific point

Php Code

$sql="SELECT * FROM sitemap";
$row = mysql_fetch_array($result);

$lastid=$row[0]; //This is the value of the last URL id that was entered successfully to sitemap in my case its on 1 as its starting from begining

$sql_url=mysql_query("SELECT * FROM books ORDER BY id ASC LIMIT $lastid, 50");

//Using the loop to store the urls
while($row_url = mysql_fetch_array($sql_url))
$pattern = "/[^A-Za-z0-9\-]/";
$url_row2=str_replace(' ','-',$row_url[2]);
$url_row3=str_replace(' ','-',$row_url[3]);
$url_row2= preg_replace($pattern,'',$url_row2);
$url_row3= preg_replace($pattern,'',$url_row3);

// I am also stuck here
// This is how my .xml file is right now, I somehow need to read the <!-- URLS Below --> and add the xml urls below that, not sure how to do that in php
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<!-- URLS Below -->


$fp = fopen ("/site/sitemap.xml", "w");
//help needed here

//update the last done field
mysql_query("UPDATE sitemap SET last_done ='.$lastdone.'");

3:39 pm on June 12, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:July 21, 2008
votes: 0


The way I do my automated sitemaps in PHP is:-

- setup a mod_rewrite rule to point sitemap.xml -> sitemap.php

- have a 'lastmod' column on pages/books which is automatically updated on every edit (via mysql UPDATE CURRENT_TIMESTAMP)


- output the XML sitemap in PHP

- order it homepage, categories, products, pages

This way you won't need a cronjob - it's run on the fly

7:20 pm on June 24, 2009 (gmt 0)

Full Member

10+ Year Member

joined:June 1, 2007
votes: 0

Does it make a difference if the extension is xml or php?

I have an auto generated site map too but I just point to the php extension and make sure it outputs the header in the php.

header("Content-Type: application/xml; charset='utf-8'");