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