Organizational Research By

Surprising Reserch Topic

php check if the page run on mobile or desktop browser using -'php,html,css,mobile'


php check if the page run on mobile or desktop browser  using -'php,html,css,mobile'

In my PHP page I should display two different text contents according to whether the page run under mobile or desktop browser. Is there a way to perform this control in PHP?
    

asked Sep 8, 2015 by rajesh
0 votes
31 views



Related Hot Questions

3 Answers

0 votes
There is a very nice PHP library for detecting mobile clients here: http://mobiledetect.net Using that it's quite easy to only display content for a mobile: include 'Mobile_Detect.php'; $detect = new Mobile_Detect(); // Check for any mobile device. if ($detect->isMobile()) // mobile content else // other content for desktops
answered Sep 8, 2015 by rajesh
0 votes
I used Robert Lee`s answer and it works great! Just writing down the complete function i'm using: function isMobileDevice(){ $aMobileUA = array( '/iphone/i' => 'iPhone', '/ipod/i' => 'iPod', '/ipad/i' => 'iPad', '/android/i' => 'Android', '/blackberry/i' => 'BlackBerry', '/webos/i' => 'Mobile' ); //Return true if Mobile User Agent is detected foreach($aMobileUA as $sMobileKey => $sMobileOS){ if(preg_match($sMobileKey, $_SERVER['HTTP_USER_AGENT'])){ return true; } } //Otherwise return false.. return false; }
answered Sep 8, 2015 by rajesh
0 votes
You can do it manually if you want. Reference: http://php.net/manual/en/function.get-browser.php preg_match('/windows|win32/i', $_SERVER['HTTP_USER_AGENT']) preg_match('/iPhone|iPod|iPad/', $_SERVER['HTTP_USER_AGENT']) You can even make it a script $device = 'Blackberry' preg_match("/$device/", $_SERVER['HTTP_USER_AGENT']) Here is somewhat of a small list '/windows nt 6.2/i' => 'Windows 8', '/windows nt 6.1/i' => 'Windows 7', '/windows nt 6.0/i' => 'Windows Vista', '/windows nt 5.2/i' => 'Windows Server 2003/XP x64', '/windows nt 5.1/i' => 'Windows XP', '/windows xp/i' => 'Windows XP', '/windows nt 5.0/i' => 'Windows 2000', '/windows me/i' => 'Windows ME', '/win98/i' => 'Windows 98', '/win95/i' => 'Windows 95', '/win16/i' => 'Windows 3.11', '/macintosh|mac os x/i' => 'Mac OS X', '/mac_powerpc/i' => 'Mac OS 9', '/linux/i' => 'Linux', '/ubuntu/i' => 'Ubuntu', '/iphone/i' => 'iPhone', '/ipod/i' => 'iPod', '/ipad/i' => 'iPad', '/android/i' => 'Android', '/blackberry/i' => 'BlackBerry', '/webos/i' => 'Mobile' Browsers '/msie/i' => 'Internet Explorer', '/firefox/i' => 'Firefox', '/safari/i' => 'Safari', '/chrome/i' => 'Chrome', '/opera/i' => 'Opera', '/netscape/i' => 'Netscape', '/maxthon/i' => 'Maxthon', '/konqueror/i' => 'Konqueror', '/mobile/i' => 'Handheld Browser'
answered Sep 8, 2015 by rajesh

...