Jump to content

'headers already sent' error with php redirect


IanS

Recommended Posts

Back again with same task but moved on half a step.Trying to redirect to mobile page using php.I assumed the code here would redirect if the match was true, or continue to load the page (index.php) if not. <?php $agent = $_SERVER['HTTP_USER_AGENT']; if(preg_match('/iPhone|Android|Blackberry/i', $agent)){ header("Location: mobile.html"); exit; } ?>But is there some boolean weirdness? I'm getting this error - Warning: Cannot modify header information - headers already sent by (output started at /mysite/index.php:1) in /mysite/index.php on line 4index.php on line 4?(If the match isn't true (not a mobile device) the page (index.php) loads ok.)

 

Link to comment
Share on other sites

That error means it can't send the header because it already sent output, which causes all headers to go out. The output started on index.php at line 1. If you have that file saved as a UTF file, make sure to save it without the BOM.

Link to comment
Share on other sites

That was it.

 

Works now :D

 

So folks are happy, and I'm off to buy some necessary books.

Thanks

 

(I remember that from when I first set up a phpbb forum ages ago and had a non-functioning site. If I'd understood the error I might have remembered it.)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...