PHP: How to detect the device used by a user?

iOS
if (stripos($_SERVER['HTTP_USER_AGENT'],"iPod") || stripos($_SERVER['HTTP_USER_AGENT'],"iPhone") || stripos($_SERVER['HTTP_USER_AGENT'],"iPad"))
	$device = 'ios';
Android
if (stripos($_SERVER['HTTP_USER_AGENT'],"Android") )
	$device = 'android';
LG Mobile
if (stripos($_SERVER['HTTP_USER_AGENT'],"webOS"))
	$device = 'webOs';