简体   繁体   English

如果移动设备使用Javascript中的PHP

[英]PHP in Javascript if mobile device

i have question, that i can't answer by myself. 我有一个问题,我自己不能回答。 I want to do the PHP code if a visitor is using mobile device. 如果访客正在使用移动设备,我想做PHP代码。 But, the problem is, that it execute the PHP code even if I'm one the PC. 但是,问题是,即使我是一台PC,它也会执行PHP代码。

  if( /Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
      document.write("
        <?php 

            if(isset($_COOKIE['web'])){
                /* nothing */
            }
            else {
                header("Location: /choose.php");
            }
        ?>
      ");
  }

Idea is to use PHP for detecting device, but some people told me that it's not working properly. 想法是使用PHP来检测设备,但有人告诉我它无法正常工作。 Thank's for response ! 感谢您的回应!

Sure. 当然。 Because your php code is executed on the server side anyway. 因为无论如何,您的php代码都是在服务器端执行的。 And javascript on the client side. 和客户端上的javascript。

You can sniff the user agent server-side too: 您也可以在服务器端嗅探用户代理:

if( preg_match("/Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i",$_SERVER['HTTP_USER_AGENT']) && !isset($_COOKIE['web'])) {
    die(header("Location: /choose.php"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM