ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

php redirect for mobile vs non-mobile

October 03, 2009 8:42pm

Subscribe [3]
  • #1 / Oct 03, 2009 8:42pm

    gguschke

    6 posts

    Hi, I’m using PHP to detect whether a user is browsing with a mobile device or not, and then redirect them either to another ‘normal browsing’ page or a mobile-friendly page. I’ve turned PHP on for this template. I get a ‘200 OK’ result that gives a link to the correct page, but why won’t the browser go to that location automatically?

    I’m open to suggestions of completely different ways to accomplish the redirects. What code are other people using for this?

    Link to the template in action: test template

    This is the only code in the template. (For demo purposes, mobile should go to apple.com and non-mobile should go to google.com)

    <?php
     
    $mobile_browser = '0';
     
    if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
        $mobile_browser++;
    }
     
    if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
        $mobile_browser++;
    }    
     
    $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
    $mobile_agents = array(
        'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
        'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
        'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
        'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
        'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
        'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
        'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
        'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
        'wapr','webc','winw','winw','xda','xda-');
     
    if(in_array($mobile_ua,$mobile_agents)) {
        $mobile_browser++;
    }
     
    if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
        $mobile_browser++;
    }
     
    if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
        $mobile_browser=0;
    }
     
    if($mobile_browser>0) {
       // do something
       // YES MOBILE
        header( 'Location: <a href="http://apple.com">http://apple.com'</a> ) ;
    }
    
    else {
       // do something else
       // NOT MOBILE
       header( 'Location: <a href="http://google.com">http://google.com'</a> ) ;
    }   
     
    ?>
  • #2 / Oct 03, 2009 8:55pm

    John Henry Donovan

    12339 posts

    gguschke,

    Welcome to the forums. This question is more appropriate for the ‘How to’ forum so I will move it there for you.

  • #3 / Oct 03, 2009 10:33pm

    grrramps

    2219 posts

    Sorry, no answer (seem to be quite a few ways to do a redirect; .htaccess, Javascript, et al), but another question—How do you handle a non-redirect for mobile users that don’t want to go to the mobile site, but that want to visit the regular site?

  • #4 / Oct 03, 2009 11:10pm

    gguschke

    6 posts

    Good question. I want to be able to allow for that situation, but I’m not sure of a method that works best with Expression Engine templates. Any recommendations?

  • #5 / Oct 04, 2009 12:04am

    grrramps

    2219 posts

    Good question. I want to be able to allow for that situation, but I’m not sure of a method that works best with Expression Engine templates. Any recommendations?

    Not for an auto detect and redirect.

    Apple (iPhone accounts for the vast majority of mobile browsing) does not recommend auto redirection to a mobile version of a site. Of course, that’s just one platform for mobile browsing (though the most used).

    That said, I’ve created a few mobile versions of sites which make it easier and faster to browse, but leave it to the browser user to click or bookmark, rather than auto detect and redirect.

  • #6 / Oct 04, 2009 12:33am

    gguschke

    6 posts

    True that auto-redirecting isn’t always the best choice. I’m hoping to figure out why the PHP isn’t working in EE as intended. It would be good to know why the ‘200 OK’ is coming up and users aren’t getting automatically bounced to the destination pages.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases