Detecting Mobile Devices through javascript or PHP

Written by Tony Lea on Aug 11th, 2010 Views Report Post

There are so many mobile browsers available today that it would be difficult to specify each one. Unfortunately, if you wanted to detect the user browser through javascript you would have to explicitly specify each browser/mobile device and which action to perform if that mobile device is detected. Fortunately enough this can be significantly simplified using some simple PHP code as shown below:

$container = $_SERVER['HTTP_USER_AGENT'];
	$mobile = false;
	$useragents = array (
		'blackberry',
		'slurp',
		'mspie',
		'pocket',
		'opera mini',
		'Series60',
		'S60',
		'iPhone',
		'iPad',
		'iPod',
		'Android'
	);
	
	foreach ( $useragents as $useragent ) {
		if(stripos($container, $useragent)) {
			$mobile = true;
		}
	}
	
	if($mobile){

	} else {
	
	}

Using the code above you can simply echo out specific HTML or javascript code based on what type of device a user is on.

Comments (0)