homepage Welcome to WebmasterWorld Guest from 54.242.18.190
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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
BlackRaven




msg:3931683
 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
=================================
<?php
require_once('config.php');
mysql_connect($server,$user,$pass);
mysql_select_db($database);

$sql="SELECT * FROM sitemap";
$result=mysql_query($sql);
$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);
$xml_url.='<url>';
$xml_url.='<loc>http://www.site.com/textbook/'.$row_url[11].'-'.$url_row2.'-by-'.$url_row3.'-ISBN:'.$row[1].'</loc>';
$xml_url.='<changefreq>never</changefreq>';
$xml_url.='</url>';
}

// 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 -->
<url>
<loc>http://www.example.com/</loc>
<changefreq>never</changefreq>
</url>

</urlset>
+++++++++++++++++++++++++++++++++++++++++++++++++

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

//update the last done field
$lastidone=$lastid+50;
mysql_query("UPDATE sitemap SET last_done ='.$lastdone.'");
?>

 

nick279




msg:3932159
 3:39 pm on Jun 12, 2009 (gmt 0)

BlackRaven

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)

- MAKE SURE YOU ENCRYPT FOR XML (&amp; etc)

- 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

bkeep




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