如果用户浏览到子域,PHP将重定向到移动版本

Scenario: I have a website set up with a php 'Mobile Detect' function. If a mobile device IS detected OR version=mobile (which sets a cookie), they are shown (via php) the mobile version of the page. If a mobile device is NOT detected, OR if the version=full (which sets a cookie), they are shown (via php) the Full version of the site. This all takes place on example.com of my domain.

I want to allow users to force the mobile version of the site (in case they have previously set the version Cookie) if they visit m.example.com.

Is there a way to redirect m.example.com to www.example.com AND set the version=mobile ? I tried doing this with my DNS settings, using a URL redirect from the 'm' subdomain to www.example.com/index.php?version=mobile which DOES work for the URL, however when the page is shown, even though the ?version=mobile is in the URL, my PHP 'Mobile Detect' does not pick up on this, and still renders the Full version of the page.

EDIT :: FYI I am on a Linux Server.

This is the php code, it is more efficiently if you use htaccess for this

$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

if ($iphone || $android || $palmpre || $ipod || $berry == true) 
{ 
   echo "<script>window.location='http://m.site.com'</script>";
}

Probably it's too late for this, but I think the problem might be with the cookie. The value of the cookie isn't changed at the moment you set it, its changed later/at the en of the script. So if you assign the cookie the value 'mobile' at the beginning of the script and then your script behaves according to it, it's value might not be yet set to 'mobile'. It's better if you write the value of the cookie in a variable at the beginning and then play with that variable, instead of relying on the cookie. So the cookie first sets the variable to 'full' (default) or NULL from the redirect, but then the $_GET sets the variable to 'mobile' and sends the order to set the cookie to 'mobile', which will be set later. Then the rest of your code should act according to the variable.

Each of your pages would have to contain the code for both the mobile and the full version, unless you pull them from a database or other means (think Pretty URLs as an example). Then you can write your code as an IF/ELSE statement.