PHP和Javascript检测Apple设备

Apple Agents

apple设置的http头中的user agents

iphone:

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3

ipad:

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) version/4.0.4 Mobile/7B367 Safari/531.21.10

ipod:

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) version/4.0.4 Mobile/7B367 Safari/531.21.10

javascript验证实例:

var Apple = {};
Apple.UA = navigator.userAgent;
Apple.Device = false;
Apple.Types = ["iPhone", "iPod", "iPad"];
for (var d = 0; d < Apple.Types.length; d++) {
	var t = Apple.Types[d];
	Apple[t] = !!Apple.UA.match(new RegExp(t, "i"));
	Apple.Device = Apple.Device || Apple[t];
}
// is this an Apple device?
alert(
	"Apple device? " + Apple.Device +
	"\niPhone? " + Apple.iPhone +
	"\niPod? " + Apple.iPod +
	"\niPad? " + Apple.iPad
);

PHP实例:

$Apple = array();
$Apple['UA'] = $_SERVER['HTTP_USER_AGENT'];
$Apple['Device'] = false;
$Apple['Types'] = array('iPhone', 'iPod', 'iPad');
foreach ($Apple['Types'] as $d => $t) {
	$Apple[$t] = (strpos($Apple['UA'], $t) !== false);
	$Apple['Device'] |= $Apple[$t];
}
// is this an Apple device?
echo
	"<p>Apple device? ", ($Apple['Device'] ? 'true' : 'false'),
	"</p>\n<p>iPhone? ", ($Apple['iPhone'] ? 'true' : 'false'),
	"</p>\n<p>iPod? ", ($Apple['iPod'] ? 'true' : 'false'),
	"</p>\n<p>iPad? ", ($Apple['iPad'] ? 'true' : 'false'),
	'</p>';

也可通过重写实现apple设置跳转:

RewriteCond %{HTTP_USER_AGENT} ^.*(iP.*$
RewriteRule ^(.*)$ http://apple.site.com [R=301]

发表评论

电子邮件地址不会被公开。 必填项已用*标注