Jump to content
IanS

'headers already sent' error with php redirect

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.)

 

Share this post


Link to post
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.

Share this post


Link to post
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.)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...