Jump to content

Why Warning "Cannot modify header information" Is NOT Fired ?


Go to solution Solved by mac_gyver,

Recommended Posts

Hi All,

I am reorganizing my code in order to avoid output before any header function.

In one of my files, there are two header('Location: anotherfile.php') lines, for when a given condition is not met. Shame on me, in both cases, the lines occur after the html has begun.

In one case, If I force the condition not to be met, the header('Location: anotherfile.php') line is executed without the warning being fired. Thanks for shedding some light on this ! I thought that even raw html could make the warning fire.

(In the other case, it is fired as expected.)

Edited by phdphd

It is not just HTML being output that causes this.  It is ANY character including a space.  The real way to avoid this is to keep ALL of your php code blocks above ALL of your html output.  Start in php mode and don't leave it until you are done with your php logic.

Also it is good practice not to end php scripts with an end tag:

 

<?php

// some code

?>
Remove all those "?>" tags. When you include scripts a spurious newline or extra bit of whitespace after the tag can trigger output, and be hard to track down.
  • Like 1

It sounds like your code also contains output buffering statements. these will have an affect on the OUTPUT of the content to the browser and could cause one instance of a header() to work and the other to not.

 

if that isn't what is causing the current symptom, you will need to post the code that reproduces the symptom and identify which header() statement in it does and which doesn't trigger the error message.

 

also, for the case where you don't get the error message, does the header() redirect work or does the code stay on the same page? it could be that the error is occurring, but the message is being hidden or suppressed.

Thanks to all of you for answering.

 

Here is a simplified -yet operational- code, to illustrate the case where the warning is NOT fired. Instead the redirect works normally.

<!DOCTYPE html>
<html>
 <head>
<meta charset="utf-8" />
</head>
<body>
<div>

<?php 
if (!isset($hello))//actually, $hello does not exist so far
		{
		header('Location: another_file.php');
		exit();
		}
?>

Shouldn't the warning be fired ?

  • Solution

For the case you have just posted, php's output buffering is turned on in the php.ini or possibly .htaccess file (if php is running as a server module) on your server. You can determine this by looking at the output from a phpinfo() statement.

IMPOSSIBLE! You have output a slew of html according to what you have posted. I believe you are NOT showing us all of your code to this point. That html can not possibly be heading to the browser and must be going to a php var or buffer at this point.

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.