By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

ob_get_contents() returns empty string

P: 1
Hi all, I need some help.

My ob_get_contents() is empty and gives me a empty file everytime. I tried different cache script it is still the same.

I have also tried
fwrite($fp, 'hello')

In this case, it successfully writes to the file. But when i use ob_start and ob_get_contents, it will be empty.

can anyone please advice. My simplified script is here

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     // Settings
  4.     $cachedir = 'cache/'; // Directory to cache files in (keep outside web root)
  5.     $cachetime = 600; // Seconds to cache files for
  6.     $cacheext = 'html'; // Extension to give cached files (usually cache, htm, txt)
  7.  
  8.     // Ignore List
  9.     $ignore_list = array(
  10.         '/rss.php',
  11.         '/search/'
  12.     );
  13.  
  14.     // Script
  15.     $page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
  16.     $cachefile = $cachedir . md5($page) . '.' . $cacheext; // Cache file to either load or create
  17.  
  18.     $ignore_page = false;
  19.     for ($i = 0; $i < count($ignore_list); $i++) {
  20.         $ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page;
  21.     }
  22.  
  23.     $cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0;
  24.     @clearstatcache();
  25.  
  26.     // Show file from cache if still valid
  27.     if (time() - $cachetime < $cachefile_created) {
  28.  
  29.         //ob_start('ob_gzhandler');
  30.         @readfile($cachefile);
  31.         //ob_end_flush();
  32.         exit();
  33.  
  34.     }
  35.  
  36.     // If we're still here, we need to generate a cache file
  37.  
  38.     ob_start();
  39.  
  40. ?>
All My HTML AND PHP CODES TO RUN THE SITE

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     // Now the script has run, generate a new cache file
  4.     $fp = @fopen($cachefile, 'w'); 
  5.  
  6.     // save the contents of output buffer to the file
  7.     @fwrite($fp, ob_get_contents());
  8.     @fclose($fp); 
  9.  
  10.     ob_end_flush(); 
  11.  
  12. ?>
Apr 20 '12 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,035
What is the return value of the call to function ob_get_contents()?

If its FALSE, then output buffering is not active...
Apr 21 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.