header function in PHP

dgewin
dgewin used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

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

Put
<?php
phpinfo();

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.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
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.
Mukesh YadavFull Stack Developer

Commented:
Can you share exact code of that page?
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Most Valuable Expert 2011
Top Expert 2016

Commented:
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.
http://php.net/manual/en/outcontrol.configuration.php#112938

Commented:
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)
Most Valuable Expert 2011
Top Expert 2016

Commented:

Author

Commented:
Hi,

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

session_register("uniqucodeuser");

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.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Yes, that function comes with a large red warning box in the online man pages.
http://php.net/manual/en/function.session-register.php

There really are not enough ways to warn people about the changes in PHP, but we tried here:
http://www.experts-exchange.com/articles/2391/PHP-Client-Registration-Login-Logout-and-Easy-Access-Control.html
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
Commented:
Thanks for your comments.

Once I removed the session_register() it fixed all of my issues, I don't have any header() issues now, it was the underlying cause of the problem.

My sessions are working fine without the register.

I will keep all that in mind for the future

Author

Commented:
I found the solution while testing my code

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial