PHP
  Home arrow PHP arrow Page 7 - Output Buffering With PHP
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Output Buffering With PHP
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 56
    2002-04-30

    Table of Contents:
  • Output Buffering With PHP
  • The Matrix Awaits
  • Start Me Up
  • Melting Down
  • Today's Forecast
  • Making It Simpler
  • The Real World
  • Zip Zap Zoom
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Output Buffering With PHP - The Real World


    (Page 7 of 9 )

    So that's the theory - now how about tossing it into the real world and seeing how it works? Take a look at the following PHP script, which demonstrates how output buffering, together with a series of judicious calls to PHP's error-handling functions, can be used to provide a simple and efficient wrapper for catching and handling errors in a script.

    <?php// use an output buffer to store page contentsob_start();?><html><head><basefont face="Arial"></head><body><h2>News</h2><?php// custom error handlerfunction e($type, $msg, $file, $line){ // read some environment variables // these can be used to provide some additional debuginformation global $HTTP_HOST, $HTTP_USER_AGENT, $REMOTE_ADDR, $REQUEST_URI; // define the log file $errorLog = "error.log"; // construct the error string $errorString = "Date: " . date("d-m-Y H:i:s", mktime()) . "\n"; $errorString .= "Error type: $type\n"; $errorString .= "Error message: $msg\n"; $errorString .= "Script: $file($line)\n"; $errorString .= "Host: $HTTP_HOST\n"; $errorString .= "Client: $HTTP_USER_AGENT\n"; $errorString .= "Client IP: $REMOTE_ADDR\n"; $errorString .= "Request URI: $REQUEST_URI\n\n"; // log the error string to the specified log file error_log($errorString, 3, $errorLog); // discard current buffer contents // and turn off output buffering ob_end_clean(); // display error page echo "<html><head><basefont face=Arial></head><body>"; echo "<h1>Error!</h1>"; echo "We're sorry, but this page could not be displayed becauseof an internal error. The error has been recorded and will be rectifiedas soon as possible. Our apologies for the inconvenience. <p> <ahref=/>Click here to go back to the main menu.</a>"; echo "</body></html>"; // exit exit();}// report warnings and fatal errorserror_reporting(E_ERROR | E_WARNING);// define a custom handlerset_error_handler("e");// attempt a MySQL connection$connection = @mysql_connect("localhost", "john", "doe");mysql_select_db("content");// generate and execute query $query = "SELECT * FROM news ORDER BY timestamp DESC";$result = mysql_query($query, $connection);// if resultset existsif (mysql_num_rows($result) > 0){?> <ul><?php // iterate through query results // print data while($row = mysql_fetch_object($result)) {?> <li><b><?=$row->slug?></b> <br> <font size=-1><i><?=$row->timestamp?></i></font> <p> <font size=-1><?php echo substr($row->content, 0, 150); ?>... <ahref=story.php?id=<?=$row->id?>>Read more</a></font> <p><?php }?></ul><?php}else{ echo "No stories available at this time";}// no errors occured// print buffer contentsob_end_flush();?></body></html>
    In this case, the first thing I've done is initialized the output buffer via a call to ob_start() - this ensures that all script output is placed in a buffer, rather than being displayed to the user. This output may be dumped to the standard output device at any time via a call to ob_end_flush().

    Now, whenever an error occurs, the custom error handler, cleverly named e(), will first flush the output buffer, then send a custom error template to the browser and terminate script execution. So, even if there was a Web page being constructed on the fly when the error occurred, it will never see the light of day, as it will be discarded in favour of the custom error template. If, on the other hand, the script executes without any errors, the final call to ob_end_flush will output the fully-generated HTML page to the browser.

    More PHP Articles
    More By Harish Kamath, (c) Melonfire


     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT