1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

The Perfect PHP On-the-fly Sitemap Builder Class

Discussion in 'Google Sitemaps' started by cssorized, Dec 16, 2007.

  1. #1

    I've created a PHP5 class that makes creating sitemaps (especially indexed sitemaps) quick and simple.
    It manages character escaping, setting the HTTP Content-Type to application/xml and GZIP compression for indexed sitemaps.

    You can view the highlighted code and get source file from the attached zip.

    If you use this class and you're experienced in black hat seo, I'd appreciate some IM time to get your recommendations and avoid trial and error with some sites I have.

    Problems and how it solves them with examples:

    Problem#1: I want a quick sitemap for a site with less than 50000 pages...

    create sitemap.php:
    // require() is best for library files, you should store SitemapBuilder.php in a
    // shared location and not duplicate it for every site
    // create an instance of a SitemapBuilder, look at the source to see all the
    // documented constructor arguments available
    $smb = new SingleSitemapBuilder();
    // database code to build the paths, this example makes paths like "product/123.html"
    $result = $mysqli->query('SELECT id FROM product');
    while($row = $result->fetch_row())
    	// the add() method writes XML to the sitemap file and automatically controls when to
    	// switch to a new file (when using indexed sitemaps)
    // this class uses a "destructor" which must be called, unset() ensures it is
    edit .htaccess with this new mod_rewrite rule:
    # Sitemap
    RewriteRule ^sitemap\.xml$ sitemap.php [L]
    Code (markup):

    Problem#2: I want an indexed sitemap for a site with greater than 50000 pages...

    create build_sitemap.php:
    // unlike previously, you need to specify the site URI, as it will not be
    // running from a web server where that information is available
    $smb = new IndexedSitemapBuilder('http://example.com/');
    $result = $mysqli->query('SELECT id FROM product');
    while($row = $result->fetch_row())
    // by default this creates a "sitemap index file"
    // named "sitemap.xml" which references the actual sitemaps
    // created by add() and is what you submit to search engines.
    Then use cron or Windows Task Scheduler to execute the above file perodically, with enough permissions to write to the target directory.

    Problem#3: I'm not running build_sitemap.php from the directory I want the sitemap files to go in...

    Simply change MultiSitemapBuilder's constructor from Problem#2 to specify the directory you want the sitemap files written to:
    $smb = new IndexedSitemapBuilder('http://example.com/', 'sitemaps/');

    Problem#4: I don't like the way Problem#1 generates the sitemap on-the-fly, my database code uses too many resources...

    Simply change SingleSitemapBuilder's constructor from Problem#1 to specify the stream URI to write the sitemap to, in this case a file path:
    $smb = new SingleSitemapBuilder('sitemap.xml');
    Then apply some of the solution from Problem#2 for running it perodically.

    Attached Files:

    cssorized, Dec 16, 2007 IP