# Solved: Windows Mobile and the web



## DrP (Jul 23, 2005)

I have some PDAs which use WM5 and I built a site which had an additional stylesheet for handheld devices. This worked fine. I've just received another PDA which uses WM6, but the same site doesn't display as it does on the WM5 PDAs. I think, to keep my sanity, it would be easier to build a mobile-specific sub-site, so was wondering how Google do this:

When I visit _www.google.co.uk_ using a PDA (WM5 or WM6) it immediately returns me _www.google.co.uk/m_. How do Google detect my device is a mobile? What are they using? Because, basically, I want to be able to do the same.

Thanks,


----------



## Sequal7 (Apr 15, 2001)

Try this one;

```
<?php

/*
PHP to detect mobile phones
*/

function detect_mobile_device(){
  
  // checks if the user agent claims to be windows but is windows mobile instead
  if(stristr($_SERVER['HTTP_USER_AGENT'],'windows')&&!stristr($_SERVER['HTTP_USER_AGENT'],'windows ce')){
    return false;
  }

  // check if the user agent gives away any tell tale signs it's a mobile browser

  if(eregi('up.browser|up.link|windows ce|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp',$_SERVER['HTTP_USER_AGENT'])){
    return true;
  }

  // check the http accept header to see if wap.wml or wap.xhtml support is claimed
  if(stristr($_SERVER['HTTP_ACCEPT'],'text/vnd.wap.wml')||stristr($_SERVER['HTTP_ACCEPT'],'application/vnd.wap.xhtml+xml')){
    return true;
  }

  // check if there are any tell tale signs it's a mobile device from the _server headers
  if(isset($_SERVER['HTTP_X_WAP_PROFILE'])||isset($_SERVER['HTTP_PROFILE'])||isset($_SERVER['X-OperaMini-Features'])||isset($_SERVER['UA-pixels'])){
    return true;
  }

  // build an array with the first four characters from the most common mobile user agents

  $a = array(
                    'acs-'=>'acs-',
                    'alav'=>'alav',
                    'alca'=>'alca',
                    'amoi'=>'amoi',
                    'audi'=>'audi',
                    'aste'=>'aste',
                    'avan'=>'avan',
                    'benq'=>'benq',
                    'bird'=>'bird',
                    'blac'=>'blac',
                    'blaz'=>'blaz',
                    'brew'=>'brew',
                    'cell'=>'cell',
                    'cldc'=>'cldc',
                    'cmd-'=>'cmd-',
                    'dang'=>'dang',
                    'doco'=>'doco',
                    'eric'=>'eric',
                    'hipt'=>'hipt',
                    'inno'=>'inno',
                    'ipaq'=>'ipaq',
                    'java'=>'java',
                    'jigs'=>'jigs',
                    'kddi'=>'kddi',
                    'keji'=>'keji',
                    'leno'=>'leno',
                    'lg-c'=>'lg-c',
                    'lg-d'=>'lg-d',
                    'lg-g'=>'lg-g',
                    'lge-'=>'lge-',
                    'maui'=>'maui',
                    'maxo'=>'maxo',
                    'midp'=>'midp',
                    'mits'=>'mits',
                    'mmef'=>'mmef',
                    'mobi'=>'mobi',
                    'mot-'=>'mot-',
                    'moto'=>'moto',
                    'mwbp'=>'mwbp',
                    'nec-'=>'nec-',
                    'newt'=>'newt',
                    'noki'=>'noki',
                    'opwv'=>'opwv',
                    'palm'=>'palm',
                    'pana'=>'pana',
                    'pant'=>'pant',
                    'pdxg'=>'pdxg',
                    'phil'=>'phil',
                    'play'=>'play',
                    'pluc'=>'pluc',
                    'port'=>'port',
                    'prox'=>'prox',
                    'qtek'=>'qtek',
                    'qwap'=>'qwap',
                    'sage'=>'sage',
                    'sams'=>'sams',
                    'sany'=>'sany',
                    'sch-'=>'sch-',
                    'sec-'=>'sec-',
                    'send'=>'send',
                    'seri'=>'seri',
                    'sgh-'=>'sgh-',
                    'shar'=>'shar',
                    'sie-'=>'sie-',
                    'siem'=>'siem',
                    'smal'=>'smal',
                    'smar'=>'smar',
                    'sony'=>'sony',
                    'sph-'=>'sph-',
                    'symb'=>'symb',
                    't-mo'=>'t-mo',
                    'teli'=>'teli',
                    'tim-'=>'tim-',
                    'tosh'=>'tosh',
                    'treo'=>'treo',
                    'tsm-'=>'tsm-',
                    'upg1'=>'upg1',
                    'upsi'=>'upsi',
                    'vk-v'=>'vk-v',
                    'voda'=>'voda',
                    'wap-'=>'wap-',
                    'wapa'=>'wapa',
                    'wapi'=>'wapi',
                    'wapp'=>'wapp',
                    'wapr'=>'wapr',
                    'webc'=>'webc',
                    'winw'=>'winw',
                    'winw'=>'winw',
                    'xda-'=>'xda-'
                  );

  // check if the first four characters of the current user agent are set as a key in the array

  if(isset($a[substr($_SERVER['HTTP_USER_AGENT'],0,4)])){
    return true;
  }
}

function detect_iphone(){
  if(eregi('iphone',$_SERVER['HTTP_USER_AGENT'])||eregi('ipod',$_SERVER['HTTP_USER_AGENT'])){
    return true;
  }
}

// example 1 - detect and redirect mobile browsers

if(detect_mobile_device()){
  header('Location: http://yoursite.com/mobile');
  exit;
}

// example 2 - detect and redirect desktop browsers

if(!detect_mobile_device()){
  header('Location: http://yoursite.com/');
  exit;
}

function detect_iphone(){
  if(eregi('iphone',$_SERVER['HTTP_USER_AGENT'])){
    return true;
  }
}

// example 3 - send iphone to specific site

if(detect_iphone()){
  header('Location: http://yoursite.com/iphone');
  exit;
}

?>
```


----------



## DrP (Jul 23, 2005)

Crikey! That's more than I'd expected! Will try it out, though.
Thanks,


----------



## Sequal7 (Apr 15, 2001)

There are really just three site variations, and the script will detect the type and redirect to the site you created.

http://yoursite.com/mobile
http://yoursite.com/
http://yoursite.com/iphone

The array is for the most common types, there are alot of types out there and this works pretty good.


----------



## DrP (Jul 23, 2005)

Thanks for that. I'd assumed the iphone would use similar technology tp the other mobiles. Is it significantly different?

Sequal7, there are two functions testing for the iphone - I've used the longer one, which brings me to my next problem. I'm going to insert this as an include to return a different stylesheet instead of redirecting to a different sub-site (mainly because it a customised b2evo skin I am working on). That means the above script will be inserted and run every page request. Is that going to slow things down?


----------



## Sequal7 (Apr 15, 2001)

The script checks the user agent, it should not slow things down at all. This is done immediately.

I think the problem with css may be that the site entry point will be called when the link is run, but I guess your .css sheet could be run then in a subdomain?

There is a difference between the two type of phones..
[WEBQUOTE="http://itmanagement.earthweb.com/columns/appleent/article.php/12070_3657946_2"]iPhone is a full mac OS system running OS X (unlike Microsoft, which has a Windows-like UI on a completely different OS for their WM phones)[/WEBQUOTE]


----------



## DrP (Jul 23, 2005)

Your code works a treat! Previously, I had the following in the header:




which meant mobile devices should read the handheld stylesheet and anything in it would overwrite the screen stylesheet. I think that's a waste of resources, because so much was repeated but changed in the mobile sheet, so it would be easier for mobiles to discard the screen sheet altogether and just use the mobile one.

What I've done is use your code to display either the link to the screen sheet or the link to mobile sheet, meaning the mobile device doesn't have to process two stylesheets. The most important thing of course, was that my WM6 device was ignoring the mobile stylesheet while my WM5 devices were using it. By only displaying a link to the relevant stylesheet, I've solved this problem. Thanks for your help!

If you want to see it in action then visit www.mrportman.co.uk, but I don't know how you'd view the source on a mobile device, so you wouldn't be able to see the link. It works though!

Oh, and going back to the iphone question. I've read it used Safari, which I think is standards compliant, so I don't need to treat it any differently to any other mobile device - meaning I can use the same mobile stylesheet.


----------



## MMJ (Oct 15, 2006)

http://blogs.msdn.com/iemobile/archive/2006/08/03/Detecting_IE_Mobile.aspx
http://developer.apple.com/document...ingforSafarioniPhone/chapter_3_section_3.html

If you are only testing for WM5, WM6, and Safari Mobile than this should work:


```
<?php
$mobileURL = 'http://example.com/m';
if (preg_match('/Windows CE|iPhone|iPod/', $_SERVER['HTTP_USER_AGENT'])
	header('Location: ' . $mobileURL);
?>
```


----------



## Sequal7 (Apr 15, 2001)

Glad it worked out for you, nice to see you used it to manipulate your own style.


----------

