Thứ Bảy, 24 tháng 11, 2012

How to Redirect Mobile Users to Your Mobile Site using PHP

Recently, I've developed the mobile version of the English Japanese Dictionary with the subdomain m.romajidesu.com, now I want an effective way to redirect mobile users to mobile version.

PHP mobile detection or JavaScript detection?

There are some considerations, the first one is you can use a client-side (javascript) or server-side redirection (PHP). Some people say that redirection using javascript is more accurate. However to me, loading the whole website until it redirect is a waste of time users and server resources, that's why I choose PHP redirection.

The PHP mobile detection techniqe

The second consideration is how to detect if a user is on a device or not. Of course I wouldn't re-invented the wheel, I googled for several techniques out there. Basically, all detection code based on some global variables, notably $_SERVER['HTTP_USER_AGENT'], to determine if a request is from a mobile device or not. There are some way to check using regular expression, array checking or just regularly if-block. To determine which one is faster, I write a code to benchmark it. The code is somewhat like this:
$start_time=microtime_float();
if (is_mobile()){    
    echo '<br/> mobile';
}else{
    echo '<br/> not mobile';
}
echo "<br/>".(microtime_float()-$start_time);
$start_time=microtime_float();
if (is_mobile1()){    
    echo '<br/> mobile';
}else{
    echo '<br/> not mobile';
}
echo "<br/>".(microtime_float()-$start_time);
Where is_mobile is a function using normal if-block checking and is_mobile1 is a function  regular expression technique. The out put of the program is as follows:
not mobile
8.1062316894531E-5
not mobile
0.00014901161193848
So in generally, the first function perform about 2x faster than the second one. I adopt the function from Russell Beattie's blog with just few modification as follows:
function is_mobile(){
    $op = isset($_SERVER['HTTP_X_OPERAMINI_PHONE'])?1:'';
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    $ac = strtolower($_SERVER['HTTP_ACCEPT']);
    if (strpos($ac, 'application/vnd.wap.xhtml+xml') !== false
        || $op != ''
        || strpos($ua, 'sony') !== false 
        || strpos($ua, 'symbian') !== false 
        || strpos($ua, 'nokia') !== false 
        || strpos($ua, 'samsung') !== false 
        || strpos($ua, 'mobile') !== false
        || strpos($ua, 'windows ce') !== false
        || strpos($ua, 'epoc') !== false
        || strpos($ua, 'opera mini') !== false
        || strpos($ua, 'nitro') !== false
        || strpos($ua, 'j2me') !== false
        || strpos($ua, 'midp-') !== false
        || strpos($ua, 'cldc-') !== false
        || strpos($ua, 'netfront') !== false
        || strpos($ua, 'mot') !== false
        || strpos($ua, 'up.browser') !== false
        || strpos($ua, 'up.link') !== false
        || strpos($ua, 'audiovox') !== false
        || strpos($ua, 'blackberry') !== false
        || strpos($ua, 'ericsson,') !== false
        || strpos($ua, 'panasonic') !== false
        || strpos($ua, 'philips') !== false
        || strpos($ua, 'sanyo') !== false
        || strpos($ua, 'sharp') !== false
        || strpos($ua, 'sie-') !== false
        || strpos($ua, 'portalmmm') !== false
        || strpos($ua, 'blazer') !== false
        || strpos($ua, 'avantgo') !== false
        || strpos($ua, 'danger') !== false
        || strpos($ua, 'palm') !== false
        || strpos($ua, 'series60') !== false
        || strpos($ua, 'palmsource') !== false
        || strpos($ua, 'pocketpc') !== false
        || strpos($ua, 'smartphone') !== false
        || strpos($ua, 'rover') !== false
        || strpos($ua, 'ipaq') !== false
        || strpos($ua, 'au-mic,') !== false
        || strpos($ua, 'alcatel') !== false
        || strpos($ua, 'ericy') !== false
        || strpos($ua, 'up.link') !== false
        || strpos($ua, 'vodafone/') !== false
        || strpos($ua, 'wap1.') !== false
        || strpos($ua, 'wap2.') !== false)
        {return true;}
        else{ return false;}
}  

The redirection code 

Now come to the fun part, which I haven't seen many people posted on the internet. What I want is, if a user visit www.romajidesu.com for the first time, he/she will be redirected to m.romajidesu.com, but if he/she somehow doesn't like the mobile version and go black to the standard website by click on the link on the bottom, he/she should not be redirected. The flowchart is somewhat as the image bellows
The following is the actually code, which can be included at the beginning of any page you want to be redirect automatically (of course you have to change the domain name)
$arr = explode('.', $_SERVER['SERVER_NAME'], 2);
$sub=$arr[0];
$need_redirect=false;
if (!isset($_SERVER['HTTP_REFERER'])){
    $need_redirect=true;
}else{
    $domain = parse_url($_SERVER['HTTP_REFERER']);   
    $host = $domain['host'];
    if (!preg_match('/romajidesu\.com/', $host)){
        $need_redirect=true;        
    }    
}
if ($need_redirect && ($sub!='m') && is_mobile() ){
    $old_url=$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
    $new_url='http://'.str_replace('www.', 'm.', $old_url);
    header("Location:".$new_url);die();
}

Không có nhận xét nào:

Đăng nhận xét