Posted on Monday 9th of April 2007 at 12:23 in Tutorials

Writing a PHP Google Sitemap generator without using fopen

The importance of the Google sitemap is a commonly discussed thing but if you don't use a pre-built solution (like Wordpress) then how do you keep your sitemap.xml file up to date? Here is a tutorial explaining how to write your own sitemap.xml generator in PHP (without using fopen).

(And yes I am fully aware the formatting of comments is rather nasty but it'll do)

The Google sitemap is a sitemap.xml file that you place in the document root of your website (which you inform Google of). It enables the popular search engine to index the pages in your site more accurately - rather than relying on the Googlebot to do all the hard work.

Automate the process
There are plugins for Wordpress that update the sitemap.xml file every time you publish (as there are for other content-managed solutions) but if you build your own site then you have to generate the file manually. Previously I've relied on giving the URL to a sitemap generator, saving it's output and uploading it to the server via FTP. I got bored of this so wrote my own generator that I could give to Google and forget about it. I'll explain how...

#1 - Set the header
Create a new file called sitemap.php. You need to set the header so that when you view the sitemap.php file it outputs as XML:



#2 - Open the connection to your database and do the query
However you store your information, it'll still be databased so the normal PHP connection/query code applies - this code is lifted directly off my site directly:



#3 - Start the XML document
Now that we've got all the current/published articles selected into a dataset we need to write the start of the XML document:



#4 - Work out the URL-Product
This is the thing that's most likely to differ depending on how you've developed your site. At Seopher.com I use URL-rewriting to associate a clean-URL with the ID of the article. Therefore what is realistically "http://www.seopher.com/viewarticle.php?id=5" becomes "http://www.seopher.com/articles/an_article_title". So the code I use to produce the URL-product is:



Whereas if you're using Wordpress-esq conventions (ie. www.seopher.com/article.php?id=5) then the code would look more like:



So all you need to do is work out how to make a real URL out of your databased content and then move on to step 5.

#5 - Output a list of your databased URLs
Now that you've worked out how to create a working URL-product of your content, it's time to output that into an XML schema that Google can make sense of.



The above code loops through the resultset and outputs the content into the XML schema that Google expects. The "lastmod" field is populated by re-formatting the timestamp you *should* have against when your article was posted. The "loc" assett is populated using the URL-product we made earlier.

#6 - Close everything down
It's just a case of closing the connection and ending the XML document.



And that's it as far as outputting everything in XML format. So you can upload the sitemap.php file to your server, load it into the browser and you should (hopefully) see a mess of all your content. View the source of the page and you should see something like:



Obviously that's my sitemap.php output (which has more than two items in it I might add) but you should see something to that effect. If you don't you'll need to troubleshoot what's causing problems. However, once you've got sitemap.php outputting something like you can see in the area above - then you can move on to step #7.

#7 - Modify the .htaccess file so sitemap.php becomes sitemap.xml
This is a crucial step because Google needs to see a SITEMAP.XML file and all you've got is SITEMAP.PHP. What you need to do is either edit or create a .htaccess file with the following logic in it:



What this does is it turns on the rewrite rule (allowing you to modify how URLs are handled, essentially) and adds the logic that allows a file.php to be intepreted as file.xml.

This now means that if you put sitemap.xml into your browser you'll be viewing the output from sitemap.php and that's crucial because now when Google looks for sitemap.xml it's viewing live data from your PHP script. This means that your sitemap.xml file will never be inaccurate.

Conclusion of what you should have
A sitemap.php file on your server that you can access by entering "www.yourwebsite.com/sitemap.php" or "www.yourwebsite.com/sitemap.xml" into your browser. This means that you now have a constantly up-to-date sitemap.xml file because you're not having to get it generated by a third party and upload it to your server.

How to improve it
My sitemap generator doesn't index my static pages (or even the homepage) because the homepage is already indexed sufficiently and I consider the other pages (contact, about etc) to be of no use to search engines. They're easily accessible from the navigation too so Googlebot shouldn't have any problems indexing them anyway.

Why it's good
Most hosts disable the use of the PHP function fopen which you need to write a physical sitemap.xml file, so this method bipasses the physical creation and instead references the PHP file as an XML document.

Hope this was useful.

 

Enjoy this article? Why not subscribe to the full RSS feed?

Add Your Comments








Comments

Showing most recent 20 of 50 comments [View all comments]

Hi everybody. Thanks for your site, it's very interesting and is worth visiting.his is truly a great read for me!! Once again, thanks for the quality articles you post on your blog!! I am always searching for informative stuff like this! I will check here often for more cool stuff. That is very nice idea and the watch is really brilliant and stylish.

Welcome to our website,and you will enjoy the sunshine service. Let's abandon all the worry,and enjoy the happy shopping travel.All the chanel watch andchanel replica watch are most in fashion boutique this year.rolex replica and chanel j12 watch and the chanel watches show your chaming temperament. In addtin, the chanel watch and the other chanel watches make you mold your unique glamour this winter.The swiss chanel watch have the high quality but low price. replica watches and replica rolex have all the style and cheapest price chanel j12 watch watches. Enjoying the fun of net purchase! chanel watchesWe will supply you the perfect service and after sale support system. Our honest will bring you absolute satisfaction.
ZSM
Sport is the best way to keep you healthy, fit and fine. Nike Kobe IV is one that gathers a lot of people, not only as fans that sit and watch others playing but also many who jump in the field and play.There is a long list of Lebron James Shoes helping out an athlete to do their best with light in weight footwear without abrasive feet.Names like Air Max 360, Nike, Reebok, Puma, and Dr. J are known in this regard, throughout the world.
Nowadays we can see that an athlete's clothing holds an important standing to give out the best performance. In sports outfits, Nike Air Max holds the most important place, Nike Air Shoes, as foot is the base of any performance whether sports or any other physical activity Shox Dream Shoes. In basketball, a lot of companies have gained fame with their awesome products.

That's awesome! I can't wait to get into it.

MBT Shoes Sale offer powerful training shoes Cheap MBT Shoes
Whoa! I am looking forward for the next updates of your awesome worship. It would be great if you have other great things

just like this. I really love it. Thanks!

can show a man not only his wealthy but also his high taste.replica rolex submariner watchesAs the society developed, many people are pay more attention to one's wearing than before, especially a watch. replica rolex submariner watches If you are wearing a famous brand watch, people will look up you, also will love to do business with you. You will be a successful man with a such luxury fashion watch.
coach handbag
coach purses
coach bags
cheap coach handbag

coach designer handbags
coach handbag
coach replica handbags
coach wallets , replica coach wallets
ed hardy handbag
ed hardy purse
ed hardy wallets
juicy handbag
cheap juicy handbags
juicy wallets
tous handbag
cheap tous handbag
tous wallets
chanel handbag
cheap chanel handbag
chanel wallet
louis vuitton handbag
cheap louis vuitton
louis vuitton wallet
gucci handbag
discount gucci handbags
gucci wallet
d&g handbag
d&g handbag bag
cheap d&g wallets
prada handbag
prada wallets
cheap prada handbags
christian audigier wallets
christian audigier handbag
christian audigier bag

christian audigier bags
fendi handbag
fendi bag
fendi wallet
miu miu handbag
cheap miu miu handbag
cheap miu miu wallets
dooney&bourke handbag
cheap dooney&bourke handbag
cheap dooney&bourke wallets
burberry handbag
cheap burberry handbags
burberry wallets
brand sunglasses
armani sunglasses
burberry sunglasses
cartier sunglasses
chanel sunglasses
Christian Dior Sunglasses
Coach Sunglasses
D&G Sunglasses
ED Hardy Sunglasses
Fendi Sunglasses
Ferragamo Sunglasses
Gucci Sunglasses
Louis Vuitton Sunglasses
Nike Sunglasses
Okey Sunglasses
Police Sunglasses
Prada Sunglasses
RayBan sunglasses
Versace Sunglasses
Wholesale coach handbags
cheap coach handbags
cheap coach handbag
coach designer handbags
replica coach handbags
new at coach
coach leather handbag
cheap coach purses
coach replica handbags
cheap coach bag
coach outlet
authentic handbags
cheap coach
coach discount
buy cheap handbags
discount cheap handbags
fashion coach handbag
authentic coach handbags
leather coach handbags
wholesale coach purses
coach online
buy coach bags
cheap coach purse
coach wholesale
discount coach bag

discount coach handbags
discount coach handbag
discounted coach handbags
discounted coach handbag
discount coach purses
replica coach handbags
discount designer handbags
designer handbags
discount coach
coach handbags on sale
cheap coach purses
coach replica bags
replica coach
clearance coach handbags
fake coach handbags
discount coach wallets
cheap coach handbag
authentic coach handbags
coach designer handbags
coach bags on sale
fake coach bags
cheap handbags wholesale
discount designer purses
cheap coach bag
authentic handbags
replica coach purses
wholesale designer handbags
buy coach handbags
coach handbags sale
coach hand bags
coach bags sale
coach bags online
fendi handbags
fashion handbags
cheap replica coach handbags
discount coach bag
wholesale coach handbag
discounted handbags
discount designer handbag
authentic coach handbag
fake coach
coach diaper bags
handbags on sale
coach wholesale
inspired handbags
fake coach purses
coach purses on sale
authentic designer handbags
coach sunglasses
replica designer handbag
ralph lauren handbags
coach luggage bags
coach handbags 2008
coach handbags online
new coach handbags
coach handbags store
authentic coach bag
spring coach bags
coachhandbags
classic coach handbag
buy coach bags
coach onsale
coach ergo handbags
coach hand bag
1sale coach bag
ladies coach handbags
coach baby bag
discount coach outlet
red coach purse
coach leather handbags
coach leather handbag
coach sling bags
coach shoulder bags
coach leather bags
hand bags
cheap coach outlet
buy cheap coach bag
buy cheap coach purses
buy cheap coach bags
buy cheap coach handbags
coach purses zoe
coach handbag zoe
coach wholesale
cheap replica coach handbags
cheap replica coach bags
buy replica coach bags
buy replica coach handbags
buy replica coach purses
china coach purses
buy china coach purses
wholesale coach purses
buy designer coach purses
buy designer coach handbags
buy designer coach bags
coach of new york
i like coach
coach nice
buy nice coach handbags
coach com
coach usa
coach make of
buy fake coach handbags
buy fake coach purses
buy fake coach bags
coach made of
cheap of coach
coach gift
coach women's
coach of women's
fake coach handbag
fake coach purse
fake coach bag
fake coach bags
fake coach purses
wholesale designer coach handbags
real coach handbags
buy real coach handbags
usa coach handbags






Most hosts disable the use of the PHP function fopen which you need to write a physical sitemap.xml file, so this method bipasses the physical creation and instead references the PHP file as an XML document.

That's awesome! I can't wait to get into it.

Very happy I can comment here!
Best wishes for this year.
just want to add a comment
Spring is near,every girl wants to be the bride in the special season.They are eager to put on beautiful Wedding Dresses or the Bridal gowns.During the day the Wedding gowns is the good choose,and the night,if you want radiant,you need the Evening gowns.About the bridesmaid,they have to wear Bridesmaid Dresses in order to avoid grab limelight with the bride.And the Flower Girl has the Flower Girl Dresses,too.In the wedding,the Cocktail Dresses and the Evening Dress is necessary,too!And remember,the Wedding Dress of the Bridal Dress must be the most glaring!
That’s something,That's what I was thinking.Brilliant idea.
That’s something,That's what I was thinking.Brilliant idea.
Fancy knowing that.I'm counting on you.
We can even think of gifting it someone because these replica Rolex watches also provides a wide range of models to choose from like Daytona replica Rolex watch,Swiss Rolex replica watch etc. Particularly when compared to the high price of a genuine Rolex, a Replica fake replica rolex watches watch shows itself to be a true value.
agreeable article
Thank you very much!
Your article is very useful!
The Best Discount
you can click here to know more:
http://www.shoesiii.com; --UGG boots,timberland boots
http://www.mylacoste.com/ --NFL jerseys
http://www.ghdlinks.com --hair straightener flat
http://www.hibose.com/ --Bose In-Ear Headphones
Timberland Roll-Top Boots menwaterproof leather for comfort, durability and abrasion resistance; Direct-attach, seam-sealed, waterproof construction keep feet dry in any weather ; Padded collar for a comfortable fit around the ankle and help keep out debris ; When rolled down the leather lining is exposed ; Durable laces with Taslan? fibers and rustproof hardware for long-lasting wear ; Footbed and inside of shoe is completely lined with soft, breathable leather ; Non-marking, rubber lug outsole for traction and durability
Fashion News alway take me some ideas,becaust it ,I fell vey cool.If you want to buy some fashion clothes,you can come to see it.

Subscribe to the RSS Feed

Stay up to date with Seopher.com by subscribing to the RSS feed, either in your browser or subscribe via email using the form below

Updates by Email

By subscribing by email you’re also subscribing to the Seopher.com newsletter; a periodical email outlining new reviews, competitions and other subscriber-only content

  • Wordpress Campaign Manager
  • 125x125 banner only $50 pcm
Want to give your product/website exposure?

Paying for a featured review is a great way to give your product, service or website exposure. For as little as $50 you can have a full review on the site forever.

Advertising Bundle! Review + Banner = $70

To kick start the new improved Seopher.com, buy a review and get a 125x125 advert half price. Your banner gets displayed on over 450 pages for a full month.