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()
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
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..
heres what a user will see if he looks at your source
Now all your source code will be
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>
- Compressed
- Harder to read
- works with 90% of browsers