PHP: Mobile Device Detection Script

Kalo kita punya website dengan dua versi, versi desktop dan versi mobilenya, pasti kita mau mobile user alias user yang akses pake handphone di-redirect ke website versi mobile. Gmana caranya? Gini kira-kira caranya.

[sourcecode language=”php”]

if(mobile_detect() == true){

// redirect http://m.websitekita.com

}

[/sourcecode]

fungsi mobile_detect() yang ada diatas bisa diambil dibawah ini. please use, copy, edit this simple script as you please.

[sourcecode language=”php”]

function mobile_detect()
{
$is_mobile = false;

$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)
{
$is_mobile = true;
}else{
$is_mobile = false;
}
return $is_mobile;
}

[/sourcecode]

ariawan

Web Developer, Blogger, Internet Savvy, Tech Enthusiast

You may also like...