Tomas Smith
Tomas Smith

Reputation: 427

redirecting based on device (server-side)

Is there any way to redirect user to subdomain when he is using iPhone and then redirect to a different subdomain when he is using android, this all using php?

Upvotes: 1

Views: 659

Answers (2)

mrdc
mrdc

Reputation: 716

Yes, you can use the $_SERVER global array to detect various information about a request such as the request headers. In particular, you can parse out the User Agent string to determine the browser connecting to your PHP application and redirect based on this information.

The PHP get_browser function documentation here has a good example:

<?php
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";

$browser = get_browser(null, true);
print_r($browser);
?>

Using the above you might craft your own functions like isAndroid or isIphone to detect the mobile device by the user agent string.

function isAndroid()
{
    if(preg_match('/android/i', $browser))
        return true;
    return false;
}

function isIphone()
{
    if(preg_match('/iphone/i', $browser))
        return true;
    return false;
}

For reference, an iPhone user agent string will looks something like: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a

And an Android's user agent string will look like: Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17

Hope this helps!

Upvotes: 1

user530229
user530229

Reputation:

I believe you're looking for php's get_browser and $_SERVER.

Either or both of these will help you determine the browser a visitor is using. From there, it's just a matter of sending header with the appropriate location.

Upvotes: 0

Related Questions