User agent detect with PHP

Small PHP code snippet to make a quick detect if the browser that requests your page is on a mobile…

Novembre 7, 2013


Small PHP code snippet to make a quick detect if the browser that requests your page is on a mobile device.

function isMobile(){
	$device = '';
	if( stristr($_SERVER['HTTP_USER_AGENT'],'ipad') ) {
		$device = "ipad";
	} else if( stristr($_SERVER['HTTP_USER_AGENT'],'iphone') ) {
		$device = "iphone";
	} else if( stristr($_SERVER['HTTP_USER_AGENT'],'blackberry') ) {
		$device = "blackberry";
	} else if( stristr($_SERVER['HTTP_USER_AGENT'],'android') ) {
		$device = "android";
	}
	if( $device ) {
		return $device; 
	} 
	return false; 
}

If it’s a mobile device (that is to say an iphone, an ipad, an android or a blackberry) returns the $device variable. Else it returns false.

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.