Avatar of dgewin
dgewin
Flag 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.
PHPWindows Server 2012Web Development

Avatar of undefined
Last Comment
dgewin

8/22/2022 - Mon
Julian Hansen

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 Baldwin

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 Yadav

Can you share exact code of that page?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ray Paseur

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
gr8gonzo

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.
Bernard Savonet

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)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

dgewin

ASKER
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.
Ray Paseur

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:
https://www.experts-exchange.com/articles/2391/PHP-Client-Registration-Login-Logout-and-Easy-Access-Control.html
Your help has saved me hundreds of hours of internet surfing.
fblack61
Bernard Savonet

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?
Bernard Savonet

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
ASKER CERTIFIED SOLUTION
dgewin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dgewin

ASKER
I found the solution while testing my code
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.