homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Help With Creating An Auto Gen Sitemap

10+ Year Member

Msg#: 3931681 posted 12:26 am on Jun 12, 2009 (gmt 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.'");



5+ Year Member

Msg#: 3931681 posted 3:39 pm on Jun 12, 2009 (gmt 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


5+ Year Member

Msg#: 3931681 posted 7:20 pm on Jun 24, 2009 (gmt 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'");

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved