移动检测失败

Mobile detection failing

我正在尝试检测我的用户是否在使用智能手机。我尝试了很多不同的方法,但似乎没有检测到它是否是智能手机。

下面那个连回显都没有$_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;
        }
?>

请检查解决方案:

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')
){}