phdphd Posted August 31, 2016 Share Posted August 31, 2016 (edited) 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 August 31, 2016 by phdphd Quote Link to comment https://forums.phpfreaks.com/topic/302055-why-warning-cannot-modify-header-information-is-not-fired/ Share on other sites More sharing options...
ginerjm Posted August 31, 2016 Share Posted August 31, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302055-why-warning-cannot-modify-header-information-is-not-fired/#findComment-1536920 Share on other sites More sharing options...
gizmola Posted August 31, 2016 Share Posted August 31, 2016 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/302055-why-warning-cannot-modify-header-information-is-not-fired/#findComment-1536923 Share on other sites More sharing options...
mac_gyver Posted August 31, 2016 Share Posted August 31, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302055-why-warning-cannot-modify-header-information-is-not-fired/#findComment-1536924 Share on other sites More sharing options...
maxxd Posted September 1, 2016 Share Posted September 1, 2016 I'm with mac_gyver on this - sounds like there's buffering going on somewhere in the system - are you using a CMS or framework, or is this a straight php environment? Quote Link to comment https://forums.phpfreaks.com/topic/302055-why-warning-cannot-modify-header-information-is-not-fired/#findComment-1536943 Share on other sites More sharing options...
phdphd Posted September 1, 2016 Author Share Posted September 1, 2016 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/302055-why-warning-cannot-modify-header-information-is-not-fired/#findComment-1536949 Share on other sites More sharing options...
Solution mac_gyver Posted September 1, 2016 Solution Share Posted September 1, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302055-why-warning-cannot-modify-header-information-is-not-fired/#findComment-1536950 Share on other sites More sharing options...
ginerjm Posted September 1, 2016 Share Posted September 1, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302055-why-warning-cannot-modify-header-information-is-not-fired/#findComment-1536951 Share on other sites More sharing options...
phdphd Posted September 1, 2016 Author Share Posted September 1, 2016 And the winner is .... mac_gyver (output buffering is turned on in the php.ini file). Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/302055-why-warning-cannot-modify-header-information-is-not-fired/#findComment-1536974 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.