/    Sign up×
Community /Pin to ProfileBookmark

Strange behaviour with header related functions

I’m running the same version of a site on my local development machine and an online staging server. Setting redirects (header: Location) and cookies works fine through my local machine, but on the remote server cookies often aren’t set (but sometimes are), and redirects rarely work.

What happens is the redirect half works – it goes to the page in question (as I can see from the address bar) but an empty page is returned. I then have to click the ‘Go’ button and it forwards as it should.

For example, on ‘submit’ of a form, the user goes to login.php, which logs them in of course, then forwards them to home.php. This works on the local server but on the remote server it will stop at login.php until I press ‘Go’ again.

Note: There’s no error being returned, and I’m not passing any whitespace or HTML etc. before the redirect/cookie set etc.

Does anyone have any recommendations?

Many thanks

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Mar 20.2007 — I'm not sure my answer will help but I once had a similar problem. I solved it by ensuring that each redirecting [B]header()[/B] was followed by [B]exit();[/B].

When a header() instruction is issued, the pertinent information is sent to the client, your browser. However, the server continues processing the script from which it was issued, as header() doesn't interrupt server-side processing. As the new page loads into the browser and the old page is still running on the server, from server to server and client to client, things can get out of sync. I was getting this behavior - as you are - on shared hosts. My local/home server never caused a problem.

Eventually, I resolved all those issues by not using header() to redirect - or better said, structuring my applications so as not to have to use header() except where no alternatives exist.
Copy linkTweet thisAlerts:
@cdk57authorMar 20.2007 — Thanks for the reply, nightshift. I'd read a similar post before and have actually built a wrapper function for redirects which makes sure exit() is called every time, so I think it must be something else...
×

Success!

Help @cdk57 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...