Jump to content

Headers NOT Sent...?


DaveWishesHe

Recommended Posts

Hi all,

 

It's probably something obvious, but perhaps someone could explain to me what is going on here...

 

I spent a lot of time this afternoon trying to fix what seemed to be a very odd problem, which involved an image being chucked out like so:

 

<?php
header("Content-Type: image/png");
echo file_get_contents("http://www.example.com/some_image.png");
?>

 

Except the image was coming out as corrupted, ultimately because I had some white space before this snippet (foolish, I know). The reason it took me so long to diagnose the problem was because I wasn't getting PHP errors (even though they were switched on). As far as the script was concerned, it seems I wasn't sending anything before attempting to modify the headers.

 

After solving the problem, I put the following little bit of code together, trying to force a header error... And yet I get nothing. It all works, and I get redirected, when I'm sure I shouldn't be. I've run this on a default install of XAMPP, and a configured CentOS server, and the same thing happens on both.

 

If anyone can explain to me what is going on, I would be most appreciative! There are some inline comments which should help clarify what I mean.

 

<?php

error_reporting(E_ALL);
ini_set("display_errors","1");

?>

<p>Hello! The very existence of this text should really have resulted in the headers being sent... Shouldn't it?</p>
<p>So I'd expect to see a "Headers already sent" message... Shouldn't I?</p>

<?php

// Headers sent?
echo "<p>";
echo "Headers ";

if(!headers_sent()) {
	echo "not "; // This "not" does echo out.
}
echo "sent.</p>";

// This WILL start a session (or at least, won't throw an error)
session_start();

// This redirect WILL work, and you will never see the above messages.
header("Location: redirect.php");

// What?!

?>

 

Thanks!

 

Dave

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/258920-headers-not-sent/
Share on other sites

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.