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
https://forums.phpfreaks.com/topic/261305-ob_get_contents-give-empty-string/
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');

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.