1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Windows Mobile and the web

Discussion in 'Web Design & Development' started by DrP, Mar 26, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. DrP

    DrP Thread Starter

    Joined:
    Jul 23, 2005
    Messages:
    739
    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,
     
  2. Sequal7

    Sequal7

    Joined:
    Apr 14, 2001
    Messages:
    2,382
    Try this one;
    Code:
    <?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;
    }
    
    ?>
     
  3. DrP

    DrP Thread Starter

    Joined:
    Jul 23, 2005
    Messages:
    739
    Crikey! That's more than I'd expected! Will try it out, though.
    Thanks,
     
  4. Sequal7

    Sequal7

    Joined:
    Apr 14, 2001
    Messages:
    2,382
  5. DrP

    DrP Thread Starter

    Joined:
    Jul 23, 2005
    Messages:
    739
    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?
     
  6. Sequal7

    Sequal7

    Joined:
    Apr 14, 2001
    Messages:
    2,382
    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..
     
  7. DrP

    DrP Thread Starter

    Joined:
    Jul 23, 2005
    Messages:
    739
    Your code works a treat! Previously, I had the following in the header:

    <link rel="stylesheet" type="text/css" href="css/dinosaur.css" media="screen" />
    <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="css/IE7hacks.css" media="screen"> <![endif]-->
    <!--[if IE 6]> <link rel="stylesheet" type="text/css" href="css/IE6hacks.css" media="screen"> <![endif]-->
    <link href="css/mobile.css" rel="stylesheet" type="text/css" media="handheld" />

    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.
     
  8. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
  9. Sequal7

    Sequal7

    Joined:
    Apr 14, 2001
    Messages:
    2,382
    Glad it worked out for you, nice to see you used it to manipulate your own style.
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - Solved Windows Mobile
  1. Gabarshek
    Replies:
    1
    Views:
    594
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/697185

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice