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

<?php
function MobileDeviceDetection{
 $tablet_browser = 0;
 $mobile_browser = 0;
 
 if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
 $tablet_browser++;
 }
 
 if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
 $mobile_browser++;
 }
 
 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_browser++;
 }
 
 $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
 $mobile_agents = array(
 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
 'newt','noki','palm','pana','pant','phil','play','port','prox',
 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
 'wapr','webc','winw','winw','xda ','xda-');
 
 if (in_array($mobile_ua,$mobile_agents)) {
 $mobile_browser++;
 }
 
 if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'opera mini') > 0) {
 $mobile_browser++;
 //Check for tablets on opera mini alternative headers
 $stock_ua = strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])?$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']:(isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));
 
 if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
 $tablet_browser++;
 }
 }
 
 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.

<?php
$DeviceType = MobileDeviceDetection();
if($DeviceType==1)
{
 echo "This is mobile device!";
}
else if($DeviceType==2)
{
 echo "This is tablet device!";
}
else
{
 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:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

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.