Redirect in PHP not working

header("Location: http://www.website.com/");

is generally enough to redirect your page but most of the time it does not work because it requires that no data should be sent to the client before the header, or rather header should be the first thing sent to the client. To fix that problem using PHP Buffering

ob_start( );
if(condition is true)
header("Location: http://www.website.com/");
else
ob_flush();

Note that in some case even this might not work. The reason is likely something was already sent to the client before the header. Typical example would be an add-in in the browser which injects some javascript into the page. If that is the case, redirect will not occur. In that you give the user an option to manually redirect, like this

This was problem in redirect, please <a href="">redirect</a> Manually.

To better trouble shoot this problem, see page contents and you will like see something that has been spitted out. In my case it was

Updating config: {"target":"English","source":"auto","key":"84"} php

The problem was only in Safari probably because of extension. In Firefox the redirect was working fine.

Advertisements
This entry was posted in Tutorials/Tips, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s