我试图检测我的用户是否在使用智能手机。我尝试过这么多不同的方法,但似乎没有什么能检测到它是否是智能手机。
下面的那个甚至没有回显$_SERVER['HTTP_USER_AGENT'];
<?php
echo $_SERVER['HTTP_USER_AGENT'];
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
if ($iphone || $android || $palmpre || $ipod || $berry == true)
{
header('Location: http://mobile.site.com/');
}
?>这是我尝试过的另一个脚本,没有回音。
<script type="text/javascript" >
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
<?php
echo "I should redirect";
?>
}
</script>我也尝试过获得屏幕大小,但这并没有显示任何东西。
<script type="text/javascript">
document.write(screen.width);
if (screen.width <= 800) {
window.location = "http://m.domain.com";
}
</script>我还尝试了下面非常流行的php脚本,但它也对$detect没有任何影响。
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
echo $detect;
if($detect->isMobile()) {
header('Location: http://mobile.example1.com/');
exit;
}
?>发布于 2015-02-17 14:12:38
请检查该解决方案:
if( strstr($_SERVER['HTTP_USER_AGENT'],'Android') ||
strstr($_SERVER['HTTP_USER_AGENT'],'webOS') ||
strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') ||
strstr($_SERVER['HTTP_USER_AGENT'],'iPod')
){}https://stackoverflow.com/questions/28563133
复制相似问题