5ubliminal@twitter

Benefits Of Output Buffering in PHP : 5ubliminal's TellinYa

<a href="http://www.tellinya.com/art2/107/">Benefits Of Output Buffering in PHP : 5ubliminal's TellinYa</a>
Must Reads: Web Scraping | Link Farming | Code Snippets | SEO Freeware » I'm on vacation! … still alive :)
Reveal More!
What is output buffering?

PHP introduced a really nice concept that says: untill the HTML page generation ends data is stored in memory and then it is output all ot once. So, instead of sending your page in slices as it is generated, PHP sends it all at once. But if you make an express request to behave like this!

The great advantage of this method is that you can access your entire HTML before sent. And here, if you got game, possibilities are endless. You can alter your code, change the HTML and even GZip compress your output.

So PHP puts together your page in memory, shoes it to you one last time and then sends it. And when you see you can also touch ;) Best use for this is to cache pages every now and then. Like every one hour you cache your pages to mysql and send them during that one hour from cache and when one hour elapses you cache again and so on. I'll implement that here in a few days!

The learn more about output control go to PHP output control page. There are many functions and tricks but if you grasp those listed here it's as easy as it gets.

How do I integrate this?

The code below is the basic sample. It's the starting point and works like this. You call the ob_start function and use a callback function as parameter. The callback will receive as parameter the HTML of the page right before it is sent. You can then change it or just return it. When your callback function returns the HTML it is output.

Warning: In callback function any call to functions that do an output are disabled. So if you try and echo "something"; you will see it no longer works. Any output has to somehow be appended / inserted in the parameter HTML code and then returned!

Now a script to blend with the theory

The code below is already commented in the use gzip compressions on web pages tutorial. Check that code too and notice the playWithHtml function is both here and there. Use the code there and copy the function from here and have GZip output compression integrated too!

<?
// -------------------------------------------------------------------------------------
// Alter external links here!
function playWithHtml($OutputHtml){
    if(!
preg_match_all("/<a\s([^>]+)>(.*)<\/a>/Usi",$OutputHtml,$Links))
        return 
$OutputHtml;
    
$InnerHtmls=$Links[2];
    
$LinkTags=$Links[1];
    foreach(
$LinkTags as $LinkTag){
        if(
preg_match("/\srel=/i",$LinkTag) &&
            
preg_match("/\starget=/i",$LinkTag)
        ) continue;
        
// Only add nofollow and target blank on external links!
        
if(!preg_match("/href=[\"']?http/i",$LinkTag)) continue;
        
$OldLinkTag=$LinkTag;
        if(!
preg_match("/\starget=/i",$LinkTag)){
            
$LinkTag=trim($LinkTag)." target=\"_blank\"";
        }
        if(!
preg_match("/\srel=/i",$LinkTag)){
            
$LinkTag=trim($LinkTag)." rel=\"nofollow\"";
        }
        
$OutputHtml=str_replace($OldLinkTag,$LinkTag,$OutputHtml);
    }
    return 
$OutputHtml;
}
// -------------------------------------------------------------------------------------
function obOutputHandler($OutputHtml){
    return 
playWithHtml($OutputHtml);
}
ob_start("obOutputHandler");
// -------------------------------------------------------------------------------------
?>
<a href="internal.html">the internal ignored link!</a>
<a href="http://www.google.com/">external link!</a>
<a href='http://www.google.com/'>external link!</a>
<a href=http://www.google.com/>external link!</a>

The code above outputs the following HTML. Notice the rel nofollows and target blanks addition to the above HTML.

<a href="internal.html">the internal link!</a>
<a href="http://www.google.com/" target="_blank" rel="nofollow">external link!</a>
<a href='http://www.google.com/' target="_blank" rel="nofollow">external link!</a>
<a href=http://www.google.com/ target="_blank" rel="nofollow">external link!</a>

Last but not least …

Hope you enjoy the scripts and use them. And if you like them link to them! Thanks.

6 Comments Posted By Readers :

Add your comment
#1 m0nkeymafia from Great Britain
Posted on Tuesday, 15 January, 2008
So did the caching of this site improve anything at all?
#2 5ubliminal web
Posted on Tuesday, 15 January, 2008
I have not added caching yet as it was not needed. It would actually cause me some problems on how the site is designed.
But if I ever get on Digg's front page again I will add it.

It's only viable if you have 1000s of daily visitors and your server shows aging signs.
And trust me, it does benefit as no SQL and other sutff would be involved anymore. Just a file reading so you can handle a lot more connections simultaneously.

Subscribe to RSS as I'll post an article soon on how to add caching to your site … step by step guide.
#3 m0nkeymafia from Great Britain
Posted on Tuesday, 15 January, 2008
Already on the RSS mate, strangely added you ages ago but never read, just going through the old posts and liking what I'm reading. Especially as you have a similar background to me, C++ should be the base for any "real" programmer haha!!

Do you have a good way to determine bottlenecks on your site? I usually have to resort to make-shift techniques, i.e. calculate time of page execution and email me when a page takes too long, I can then pick up the email and try analyse the specific page.

I would implement caching on my site, but all the popular pages are highly dynamic so normal caching methods won't work too well. Instead I use a combination of Ajax and optimised queries to make things as fast as poss, mind you I'm open to any further ideas you may have!
#4 5ubliminal web
Posted on Tuesday, 15 January, 2008
Actually it's coded rather well and have no problems with it. I split everything in includes and time their execution when I feel like testing.
All my pages start from a root page that handles all includes and all my site is actually one page that included different body parts.
But I usually code really clean upfront so … it works well. (Even if a Digg knocked me dead!)

On highly dynamic pages, caching is really easy. As long as you have access to output buffering and local write access :) … it's as good as done. I'll post a complete method in a few days.
PS: Yes! Ajax does help. I'm actually rewriting this site with a lot of Ajax right now.
#5 m0nkeymafia from Great Britain
Posted on Tuesday, 15 January, 2008
Ah see I used to do the whole, 1 page loads all scenario, but opted for header / footer includes on each page now. Not sure if there is much difference between the two in terms of added functionality or speed, however a good coder person I know also uses the 1 page method... maybe I am behind the times?

Yeah I'm a big fan of clean writing, I collar anyone I work with who doesn't respect it haha.

Well see the problem with it is lots of different parts of the site change, and some very quickly, i.e. some do ajax calls every 5-10 seconds and so forth. I suppose I could create a component style architecture caching separate parts of the site individually rather than entire pages. Then I may see a decent improvement!
#6 5ubliminal web
Posted on Tuesday, 15 January, 2008
I didn't explain my self right. One page loads another which loads another which loads header, footer, menus and so on. It's all split into pieces but unitary in design.

What's you site? You didn't add any to post … make sure you add http:// ...
Post Feedback 
Name *
Mail *
URL
« Anti-Spam
» URL will only go live after a review. Comments are moderated. «
5ubliminal's TellinYa.com SEM & SEO Blog © 2007 - All rights reserved unless mentioned otherwise .
Rendered On : [Thursday, 21 August, 2008 - 21:01:58 GMT]   No Ajax / Flash Used Here
" Benefits Of Output Buffering in PHP : 5ubliminal's TellinYa "