Skip to content
WJunction - Webmaster Forum

Take full advantage of ob_start(); ... Magic

Status
Not open for further replies.
ok today i will be talking about ob_start() and how to take advantage of it in a different way than most people

here is a normal usage of ob_start()

PHP:
<?php
ob_start();
?>
<html>
<head>
<title>Ob start Basic Example</title>
</head>
<body>

<div><p>Here is just a paragraph just to act as a content filler</p></div>

</body>
</html>

Now when you run ob_start it will run threw a process of compressing your data before sending it to the web browser.

what where going to do is create our own process for it to run so we then can change the way the data is sent to the browsers.

ok so firstly we need to create a function that will take the data in then process it and then send it t the browser

PHP:
$EnableGZipEncoding = true; // if this is true, will return the html gZip'ed

function gZipAllowed(){ //this is a check to see if the browser can accept gZip
    $accept = str_replace(" ","",strtolower($_SERVER['HTTP_ACCEPT_ENCODING']));
    $accept = explode(",",$accept);
    return in_array("gzip",$accept);
}

function cleanHtml($OutputHtml){
    now inside this function we can do many things from trimming to removing newlines and carriage returns. 
    $html = preg_replace("/\s+/"," ",$OutputHtml);//shrinking spaces
    $html = preg_replace("/\r+/"," ",$OutputHtml);//shrinking returns
    $html = preg_replace("/\n+/"," ",$OutputHtml);//shrinking new lines
    return $html;
}

function LitewarezObHandler($OutputHtml){
    global $EnableGZipEncoding;
   
    $OutputHtml = cleanHtml($OutputHtml);
    
    if(! gZipAllowed() || headers_sent() || !$EnableGZipEncoding) return $OutputHtml;

    header("Content-Encoding: gzip");
    return gzencode($OutputHtml);
}
?>

Now in the LitewarezObHandler() function can do whatever we want to the html so you can create a mini templating system if you wish by replacing {home_link} in the function

....

so now the usage of this fantastic html controller..

PHP:
<?php
//make sure that the functions above are included here
ob_start('LitewarezObHandler');

//so what we are doing here is telling ob_start to pass the html data threw my special function
?>
<html>
<head>
<title>Ob start Basic Example</title>
</head>
<body>

<div><p>Here is just a paragraph just to act as a content filler</p></div>

</body>
</html>


heres what a user will see if he looks at your source
PHP:
<html><head><title>Ob start Basic Example</title></head><body><div><p>Here is just a paragraph just to act as a content filler</p></div></body></html>
Now all your source code will be
  1. Compressed
  2. Harder to read
  3. works with 90% of browsers
 

5 comments

it will make your source 1 line?
PHP does a lot of these things that arent really needed though. Like for example. when would this be "needed"?
but fasting loading times and less bandwidth is always a good thing :D
 
Litewarez that's the reason why I can't open your site because it is saying you use a invallid or unsupported method of compression. Just thought you should know.
 
this is not just for making your source for one line you can do stuff like

PHP:
<?php ob_start('LitewarezObHandler')?>
<body>
<div id="header">{_HEADER_}</div>
<div id="leftbar">{_LEFTMU_}</div>
<div id="main-content">{_CONTENT_}</div>
<div id="footer">{_FOOTER_}</div>
</body>

and in the LitewarezObHandler you can repplacewhats in the brackets with content from DB/FILE w/e
 
the problem is the bloke who im running it with is grounded like a lil kid so i dont have root access and the hard drive is full meaning all things are down from SESSIONS , MYSQL , and the the fact that my settings for compression are used throughout sessions and come fromt he database the server is running under the wrong encodeing..... all this for a lil hard drive space has driven me mad lol
 
Status
Not open for further replies.

About the author

L
Active Member · Joined
I am a skilled programmer within PHP,HTML,CSS,MYSQL and also the owner of several websites Programming, Football...
1,367
Messages
1
Reactions
38
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom