简体   繁体   中英

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. But, the problem is, that it execute the PHP code even if I'm one the PC.

  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. Thank's for response !

Sure. Because your php code is executed on the server side anyway. And javascript on the client side.

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"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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