PHP check is mobile tablet or desktop

2.81K views
no comments
29 Jul 2016 1:53 am

Now modern days we are using many type of devices to access the websites and applications.

Websites and application are showing different sizes contents and media data for smartphones, tablets and desktops like showing tiny size of logo of profile image showing for mobile, medium size showing for tablet iPad devices and the large image showing for desktop.

Lets suppose website contains large size logo and size eg: 400 x 350 pixels for desktop device but its not good if large size logo is use for mobile / tablet device. so you pass different size in img src attribute.

deviceVAL(‘Mobile’,’DeskTOP’,’tabLET’,’iPAD’).’ –DEVICE’);

I have defined a function device_VAL with 4 parameters which used for different devices to save your time, code lines if conditions and bandwidth of hosting.

Example of usage code for different size of logo for devices.

<img src="<?php echo device_VAL('logo-50px.jpg','logo-original-300px.jpg','logo-150px.jpg','logo-150px.jpg'); ?>" alt="website logo" title="website logo" />

If a user came from mobile phone then the image attribute will be like

<img src="logo-original-50px.jpg" alt="website logo" title="website logo" />

If a user came from desktop then the image attribute will be like

<img src="logo-original-300px.jpg" alt="website logo" title="website logo" />

If a user came from tablet device the image attribute will be like

<img src="logo-150px.jpg" alt="website logo" title="website logo" />

If a user came from iPad device then the image attribute will be like

<img src="logo-150px.jpg" alt="website logo" title="website logo" />

Device_val function can save multiple col classes of #bootstrap grid system

without device_val function need to add multiple devices col classes like

<div class="col-xs-12 col-sm-6 col-md-3 col-lg-2"></div>

56 characters length of a single div with multiple devices col classes

with device_val function it can save 29 characters length of a single div.

<div class="col-<?php echo device_VAL('lg-2','xs-12','md-2','md-2'); ?>"></div>

define is_desktop function to detect is desktop

function is_desktop(){
	$useragent = $_SERVER['HTTP_USER_AGENT'];
	return stripos($useragent,'mobile')===false && stripos($useragent,'tablet')===false && stripos($useragent,'ipad')===false ;
}

define is_tablet function to detect is tablet device

function is_tablet(){
	$useragent = $_SERVER['HTTP_USER_AGENT'];
	return stripos($useragent,'tablet')!==false || stripos($useragent,'tab')!==false;
}

define is_ipad function to detect is iPad device

function is_ipad(){
	$useragent = $_SERVER['HTTP_USER_AGENT'];
	return stripos($useragent,'ipad')!==false;
}

define is_mobile function to detect is mobile phone device

function is_mobile(){
	$useragent = $_SERVER['HTTP_USER_AGENT'];
	return stripos($useragent,'mobile')!==false || stripos($useragent,'nokia')!==false || stripos($useragent,'phone')!==false;
}

Complete code and function of device_VAL

function is_desktop(){
	$useragent = $_SERVER['HTTP_USER_AGENT'];
	return stripos($useragent,'mobile')===false && stripos($useragent,'tablet')===false && stripos($useragent,'ipad')===false ;
}

function is_tablet(){
	$useragent = $_SERVER['HTTP_USER_AGENT'];
	return stripos($useragent,'tablet')!==false || stripos($useragent,'tab')!==false;
}
function is_ipad(){
	$useragent = $_SERVER['HTTP_USER_AGENT'];
	return stripos($useragent,'ipad')!==false;
}
function is_mobile(){
	$useragent = $_SERVER['HTTP_USER_AGENT'];
	return stripos($useragent,'mobile')!==false || stripos($useragent,'nokia')!==false || stripos($useragent,'phone')!==false;
}


function deviceVAL($mobileVAL='',$desktopVAL='',$tabletVAL='',$ipadVAL=''){
	if(is_desktop()){
		return $desktopVAL;
	} else if(is_tablet()){
		return $tabletVAL;
	} else if(is_ipad()){
		return $ipadVAL!=null?$ipadVAL:$tabletVAL;
	} else if(is_mobile()){
		return $mobileVAL;
	}
}

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>