DaveWishesHe Posted March 14, 2012 Share Posted March 14, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258920-headers-not-sent/ Share on other sites More sharing options...
ManiacDan Posted March 14, 2012 Share Posted March 14, 2012 Do you have output buffering turned on by default or something? Quote Link to comment https://forums.phpfreaks.com/topic/258920-headers-not-sent/#findComment-1327370 Share on other sites More sharing options...
DaveWishesHe Posted March 15, 2012 Author Share Posted March 15, 2012 *facepalm*. What a rookie mistake. Many thanks... Time to plug some PHP configuration holes. Quote Link to comment https://forums.phpfreaks.com/topic/258920-headers-not-sent/#findComment-1327565 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.