Link to home
Start Free TrialLog in
Avatar of dgewin
dgewinFlag for Australia

asked on

header function in PHP

Hi everyone,

I am moving a webpage I have developed from one webserver to another (due to server upgrades).

On the new server the header commands don't work unless they are literally the only thing on the page.

On the old server AND the test server they work find. I am wondering if there is something I am missing in the PHP setup? any help would be appreciated!

Test server and production have the same version of PHP

I have checked all the things people suggest.. there is no echo's, no whitespace (even in the includes), the only one I could get to work had only the header between the 2 php tags.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Are all your header directives happening before you output any content?

It might be on the other servers output buffering is turned on and not on the new server.


Open in new window

Into a file pinfo.php - upload it and call it from your browser. Check the output buffering setting and report back.
You aren't giving us any code or a link to work with.  I must admit that I have never seen that happen even when moving from PHP 4.4 to PHP 5.3 or PHP 5.4 on different servers.
Can you share exact code of that page?
Check the settings for output buffering.  If it was on in the old server and off in the new server (the default installation setting is off) then you might try turning buffering on.
1. Use Fiddler to examine the raw response of the page and make sure the headers aren't coming but are just ignored or overridden.

2. We need to see your code.

3. If you're doing header redirects, make sure you have a die() or exit() call after the header lines.
Usual cause of this problem is that you output some html before running the header function, html being started with any character. Running php without any echo (or error message!) generates no character, and so does not start html.

Obviously you have checked that none of the usual causes is involved.

So (assuming there is no difference in output buffering), this leaves only unusual causes.
Some stupid details I have several times stumbled upon:
- never use ?> at the end of any php included file which has no html output (this would generate at least one character and so would start the html generation); this is the recommended Zend option, so most probably you are already complying with this... which just lets one final "option":
- check that none of your files has a "BOM" in utf-8 or UTF-16. This is really the killing detail,since all looks fine... but the server in some cases consider the BOM as a character, so starts generating html, etc. Not sure how you can check these BOM details. My UEstudio / UEdit shows it to me, presumably the tools you use will provide you with these details. (Not sure but I think that the standard windows notepad can also help)
Avatar of dgewin



Thank you everyone for your responses, through my testing I found that the issue was not in fact the header, it was the following line of code


For whatever reason its not working on the new server, once removed it seems to work normally again.

I find it quite odd because I had been using it for quite some time on different servers and websites for different things in the past with no issues.
Yes, that function comes with a large red warning box in the online man pages.

There really are not enough ways to warn people about the changes in PHP, but we tried here:
Altough the problem arises with session_register() rather than header(), it Is still the same problem: some character is sent/echoed before the function is used.

Having session_start as one of the first instructions in my php files has been my practice since a long time...

You have session_start(), right?
Some of my painful debugging sessions (let's call that "experience") lead me to the following practice with sessions:
1 - Always place session_start just after <?php of the main program
2 - It's OK to do $_SESSION['xxx']=yyy; but DO NOT EXPECT that later in your program if you want to use the value from $_SESSION['xxx'] it will return the correct/expected value UNLESS...
3 - As soon as you have finished assigning all your values $_SESSION['xxx']= ... or if you are just reading (not writing) the values, do a session_write_close(): this will write changes if any (thus solving the poblem in 2/ above), and also unlock some data. BUT BEWARE it seems that this creates funny things when used with ajax
Avatar of dgewin
Flag of Australia image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dgewin


I found the solution while testing my code