Jump to content

ob_get_contents give empty string


Kenttan

Recommended Posts

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

 

<?php

 

// Settings

$cachedir = 'cache/'; // Directory to cache files in (keep outside web root)

$cachetime = 600; // Seconds to cache files for

$cacheext = 'html'; // Extension to give cached files (usually cache, htm, txt)

 

// Ignore List

$ignore_list = array(

'/rss.php',

'/search/'

);

 

// Script

$page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page

$cachefile = $cachedir . md5($page) . '.' . $cacheext; // Cache file to either load or create

 

$ignore_page = false;

for ($i = 0; $i < count($ignore_list); $i++) {

$ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page;

}

 

$cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0;

@clearstatcache();

 

// Show file from cache if still valid

if (time() - $cachetime < $cachefile_created) {

 

//ob_start('ob_gzhandler');

@readfile($cachefile);

//ob_end_flush();

exit();

 

}

 

// If we're still here, we need to generate a cache file

 

ob_start();

 

?>

 

All My HTML AND PHP CODES TO RUN THE SITE

 

<?php

 

// Now the script has run, generate a new cache file

$fp = @fopen($cachefile, 'w');

 

// save the contents of output buffer to the file

@fwrite($fp, ob_get_contents());

@fclose($fp);

 

ob_end_flush();

 

?>

Link to comment
Share on other sites

Remove all your @-sign prefixes so you can see any errors that are being generated.  You should almost never use @ in your code like that.  Make sure you have error reporting enabled as well.  At the top of the script put:

 

error_reporting(-1);
ini_set('display_errors', 'On');

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.