/ Published in: PHP
PHP Mobile Detector
Expand |
Embed | Plain Text
<?php /* * Lightweight detector of mobile devices, OSs & browsers * Copyright 2012 Túbal Martín (email: [email protected]) * License: GPL2 */ { // Global vars $is_mobile = false; $is_iphone = $is_ipad = $is_kindle = false; $is_ios = $is_android = $is_webos = $is_palmos = $is_windows = $is_symbian = $is_bbos = $is_bada = false; $is_opera_mobile = $is_webkit_mobile = $is_firefox_mobile = $is_ie_mobile = $is_netfront = $is_uc_browser = false; function mobile_detector($debug = false) { global $is_mobile; // Check user agent string return; } 'is_iphone' => 'iphone', 'is_ipad' => 'ipad', 'is_kindle' => 'kindle' ); 'is_ios' => 'ip(hone|ad|od)', 'is_android' => 'android', 'is_webos' => '(web|hpw)os', 'is_palmos' => 'palm(\s?os|source)', 'is_windows' => 'windows (phone|ce)', 'is_symbian' => 'symbian(\s?os|)|symbos', 'is_bbos' => 'blackberry(.*?version\/\d+|\d+\/\d+)', 'is_bada' => 'bada' ); 'is_opera_mobile' => 'opera (mobi|mini)', // Opera Mobile or Mini 'is_webkit_mobile' => '(android|nokia|webos|hpwos|blackberry).*?webkit|webkit.*?(mobile|kindle|bolt|skyfire|dolfin|iris)', // Webkit mobile 'is_firefox_mobile' => 'fennec', // Firefox mobile 'is_ie_mobile' => 'iemobile|windows ce', // IE mobile 'is_netfront' => 'netfront|kindle|psp|blazer|jasmine', // Netfront 'is_uc_browser' => 'ucweb' // UC browser ); foreach ($groups as $group) { foreach ($group as $name => $regex) { global $$name; $is_mobile = $$name = true; break; } } } // Fallbacks if ($is_mobile === false) { $regex = 'nokia|motorola|sony|ericsson|lge?(-|;|\/|\s)|htc|samsung|asus|mobile|phone|tablet|pocket|wap|wireless|up\.browser|up\.link|j2me|midp|cldc|kddi|mmp|obigo|novarra|teleca|openwave|uzardweb|pre\/|hiptop|avantgo|plucker|xiino|elaine|vodafone|sprint|o2'; ) { $is_mobile = true; } } // DEBUGGER OUTPUT if ($debug === true) { foreach ($GLOBALS as $k => $v) { } } } } // execute inmmediatly mobile_detector(); }
You need to login to post a comment.
