Detect Mobile Devices PHP Code

These days, all most web developers understand the need to optimize or responsive website for mobile devices. Usually we can do easily implementation of responsive CSS to create a great browsing experience for users of every screen size.

Most of big web projects have a desktop site version as well as mobile or tablet site version. When the visitors visit your website from a mobile device, they need to be redirected to the mobile website otherwise open the desktop website.
I am not talking about changing the actual content of web page for mobile visitors, we just change the layout, visualization of website and make load fast and remove or hide unused script make web page work better for mobile visitors?

We have made a simple function MobileDeviceDetection() for detect mobile devices, tablets and desktop in PHP.
Also you would be able to redirect mobile users to the mobile site and hide the unused script.

PHP Code For Detect Mobile Devices PHP Code

function MobileDeviceDetection{
 $tablet_browser = 0;
 $mobile_browser = 0;
 if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
 if (preg_match('/(up.browser||mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
 if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
 $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
 $mobile_agents = array(
 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
 'wapr','webc','winw','winw','xda ','xda-');
 if (in_array($mobile_ua,$mobile_agents)) {
 if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'opera mini') > 0) {
 //Check for tablets on opera mini alternative headers
 if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
 if ($mobile_browser > 0) {
 // do something for mobile devices
 return "1";
 else if ($tablet_browser > 0) {
 // do something for tablet devices
 return "2";
 else {
 // do something for everything else
 return "3";

How to use?

Just create php file mobiledevice.php you can place the above PHP code into to the mobiledevice.php file and include file into header of website.
Then simply call MobileDeviceDetection() function to check device is mobile or not whenever you need.

$DeviceType = MobileDeviceDetection();
 echo "This is mobile device!";
else if($DeviceType==2)
 echo "This is tablet device!";
 echo "This is desktop!";

Leave a Reply

Related Posts: You may like

Extract Total Number of Image And Missing Alt Attribute using php

How to extract Heading tags using PHP from string

How to remove whitespace from string using php

Remove All Special Characters From String PHP

PHPKIDA Offering Free Tools:


Download Instagram Videos & Photos.

Download Now


Real Time Currency Converter

Convert Now


Robots.txt File Creater

Create Now


Compress yout HTML Code

Minify Now


Compress yout CSS Code

Minify Now


Compress yout JavaScript Code

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.