Senin, 12 Agustus 2013

Script PHP untuk Mendeteksi Browser Mobile HP/Tablet, Menggunakan User Agent Switcher ke Mobile Page

Script PHP untuk Mendeteksi Browser Mobile HP/Tablet, Menggunakan User Agent Switcher ke Mobile Page sangat berguna untuk membuat web yang dapat mendeteksi apakah diakses oleh Web Browser Desktop biasa atau Mobile Browser milik Handphone / Tablet. Script PHP ini penulis buat setelah menggunakan Tools User Agent Switcher pada Web Browser untuk Mengetes Respon Web Mobile Page seperti yang telah penulis terangkan pada artikel sebelumnya.

Hasil pengetesan berbagai User Agent yang populer, dengan target web yang ditampilkan yang cukup populer secara nasional diwakili detik.com dan secara internasional diwakili yahoo.com, hasilnya sebagai berikut:



User Agent    => Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus S Build/GRK39F) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Browser Name  => Android 2.3 Nexus S
Web Detection => m.detik, m.yahoo

User Agent    => Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Browser Name  => Android 4 Galaxy Nexus
Web Detection => m.detik, m.yahoo

User Agent    => Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.187 Mobile Safari/534.11+
Browser Name  => Blackberry 9900
Web Detection => m.detik, m.yahoo

User Agent    => Mozilla/5.0 (BB10; Touch) AppleWebKit/537.1+ (KHTML, like Gecko) Version/10.0.0.1337 Mobile Safari/537.1+
Browser Name  => Blackberry 10
Web Detection => m.detik, m.yahoo

User Agent    => Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13
Browser Name  => MeeGo Nokia N9
Web Detection => m.detik, m.yahoo

User Agent    => Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5
Browser Name  => iPhone iOS 4
Web Detection => m.detik, m.yahoo

User Agent    => Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3
Browser Name  => iPhone iOS 5
Web Detection => m.detik, m.yahoo

User Agent    => Mozilla/5.0 (Android; Tablet; rv:14.0) Gecko/14.0 Firefox/14.0
Browser Name  => Firefox 14 Android Tablet
Web Detection => m.detik, m.yahoo

User Agent    => Mozilla/5.0 (Android; Mobile; rv:14.0) Gecko/14.0 Firefox/14.0
Browser Name  => Firefox 14 Android mobile
Web Detection => m.detik, m.yahoo

User Agent    => Mozilla/5.0 (Linux; Android 4.1.2; Nexus 7 Build/JZ054K) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19
Browser Name  => Chrome Android Tablet
Web Detection => m.detik, m.yahoo

User Agent    => Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19
Browser Name  => Chrome Android Mobile
Web Detection => m.detik, m.yahoo.com

======================================

User Agent    => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Browser Name  => Firefox 4 Mac
Web Detection => www.detik.com, www.yahoo.com

User Agent    => Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Browser Name  => Firefox 4 Windows
Web Detection => www.detik.com, www.yahoo.com

User Agent    => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Browser Name  => Firefox 7 Mac
Web Detection => www.detik.com, www.yahoo.com

User Agent    => Mozilla/5.0 (Windows NT 6.1; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Browser Name  => Firefox 7 Mac
Web Detection => www.detik.com, www.yahoo.com

User Agent    => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
Browser Name  => Internet Explorer 7
Web Detection => www.detik.com, www.yahoo.com

User Agent    => Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)
Browser Name  => Internet Explorer 8 
Web Detection => www.detik.com, www.yahoo.com

User Agent    => Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Browser Name  => Internet Explorer 9 
Web Detection => www.detik.com, www.yahoo.com

User Agent    => Mozilla/5.0 (iPad; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5
Browser Name  => iPad iOS4 
Web Detection => www.detik.com, www.yahoo.com

User Agent    => Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3
Browser Name  => iPad iOS5 
Web Detection => www.detik.com, www.yahoo.com

User Agent    => Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML, like Gecko) Version/7.2.1.0 Safari/536.2+
Browser Name  => Playbook 2.1 
Web Detection => www.detik.com, www.yahoo.com

Berdasarkan hasil tersebut bisa dibuat script PHP yang mampu meredirect halaman web ke versi mobile jika script PHP ini mendeteksi User Agent pada browser yang mengaksesnya bertipe Mobile.

index.php => mobile-browser-detect.php => mobile.php.

Cara kerjanya adalah ketika halaman utama web yaitu index.php diakses, maka akan dipanggil (include) file mobile-browser-detect.php yang akan mendeteksi User Agent dari Browser Web yang mengaksesnya. Jika bertipe mobile maka script akan me-redirect halaman web ke file mobile.php, sebaliknya, jika tidak bertipe mobile maka halaman yang tampil tetap halaman index.php.

Script PHP mobile-browser-detect.php berisi kode program seperti ini :

<?php
$mystring = $_SERVER['HTTP_USER_AGENT'];
//echo $_SERVER['HTTP_USER_AGENT'];

$findme   = 'Android';
$pos = strpos(strtolower($mystring), strtolower($findme));

// The !== operator can also be used.  Using != would not work as expected
// because the position of 'a' is 0. The statement (0 != false) evaluates 
// to false.
$ismobile = false;

if ($pos !== false) {
     //echo "The string '$findme' was found in the string '$mystring'";
     //    echo " and exists at position $pos";
  $ismobile = true;
} else {
     //echo "The string '$findme' was not found in the string '$mystring'";
}

$findme   = 'iPhone';
$pos = strpos(strtolower($mystring), strtolower($findme));
if ($pos !== false) {
  $ismobile = true;
}

$findme   = 'Mobile Safari';
$pos = strpos(strtolower($mystring), strtolower($findme));
if ($pos !== false) {
  $ismobile = true;
}

$findme   = 'Blackberry';
$pos = strpos(strtolower($mystring), strtolower($findme));
if ($pos !== false) {
  $ismobile = true;
}

$findme   = 'MeeGo';
$pos = strpos(strtolower($mystring), strtolower($findme));
if ($pos !== false) {
  $ismobile = true;
}

if ($ismobile == true)
{
 //echo "Browser Mobile";
 header("location:mobile.php");
}
else
{
 //echo "Browser Desktop";
}
?>

Ketika dilakukan pengetesan menggunakan User Agent bertipe Browser Mobile, hasilnya akan loncat (redirect) ke halaman mobile.php :


Sebaliknya jika diakses dengan browser normal dengan User Agent bertipe Desktop maka hasilnya tetap menampilkan index.php :


Project selengkapnya silakan didownload disini. Jika kesulitan ini caranya.

Semoga bermanfaat.

Tidak ada komentar:

Posting Komentar