tags:

views:

30

answers:

2

I'm trying to redirect mobile users and am attempting the following, but it doesn't actually redirect at all...

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
//print $ua;

$search = array('windows ce', 'avantgo', 'mazingo', 'mobile', 'iphone', 't68', 'syncalot', 'blazer');

foreach($search as $sk => $sv) {
    if(preg_match('/\b'.$sv.'\b/i', $ua)) {
        header("Location: http://m.example.com");
        exit;
    }

}
+2  A: 

You might want to first try just echoing Attempting to Redirect in place of the header, so you can then see if it is trying to redirect someone. That way you can check if the first half of the function works.

Here is a blog post about something very similar to what you are doing, that uses strpos instead of preg_match, which is probably more straightforward.

If it isn't 'redirecting', AKA, the header function doesn't appear to be working, you have a whole other problem.


If the header function isn't working, try adding Firefox to the list of browsers to redirect, and then use Firebug to check the headers being sent to the browser.

The might be a problem with how PHP and Apache are interacting.

There is a chance that you have error_reporting turned off. If you do, PHP might be trying to output an error that says there is whitespace sent out before your header command.

Before the header() command, trying setting error_reporting(E_ALL);, and check if it gives an error messages to the avail of

Headers could not be sent. Headers have already been sent on file.php line 1. 

Basically, if you send any HTML, Whitespace, or pretty much any sort of output before the header() command, PHP will error as headers must be sent before the content of the page. After the first whitespace is sent, headers are sent, and you can't send anymore

You can check if headers are sent using header_sent()

if(headers_sent()){ echo "Headers Sent"; }
Chacha102
I did do that, and I'm getting the 'redirecting' message, so I know that it's trying to...
phpN00b
A: 

Make sure that the response hasn't yet been committed to the current page before you make that header call. This would happen if any text has already been written to the output stream... text, whitespace, etc.

Matt