views:

55

answers:

1

Sorry for the probably wrong title. I am writing some code to handle If-Modified-Since and If-None-Match requests as part of caching. Everything works perfect except for that PHP returns some content (an empty line) after the headers. The page content should be empty instead. The code that I am using is:

<?php
$lastmod = filemtime($f);
$etag = '"'.dechex($lastmod).'"';
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $last_mod || $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
  header('HTTP/1.1 304 Not Modified');
  header('Content-Length: 0');
  exit();
}
?>
A: 

Try this code:

$last_modified = filemtime($f);
if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
    $expected_modified = strtotime(preg_replace('/;.*$/','',$_SERVER["HTTP_IF_MODIFIED_SINCE"]));
    if($last_modified <= $expected_modified) {
        header("HTTP/1.0 304 Not Modified");
        exit;
    }
}
mattbasta
My issue is not with detecting or returning proper headers, but just about an empty line outputted by php after the headers.
Ameer
Error here: http://redbot.org/?uri=http%3A%2F%2Fameer1234567890.co.cc%2Ftest%2Ftest.php
Ameer
Do you have a `register_shudown_function` set?
mattbasta
I do not have a register_shutdown_function in my code. Could it be set by my webhost. I am using 110mb.com
Ameer
It could be that you've got some weird whitespace before your opening `<?php`. Look out for those. Also, make sure all of your files are in Unix EOL format (LF, not the Windows "CR" format). If all else fails, it's your host.
mattbasta
My file doesn't have any whitespaces or characters before <?php or after ?>. I do not have a BOM too and my file is in LF format. As you said, my best suspect is my host.
Ameer