1

这是我的代码。有人遇到了 PHP 重定向的问题,所以我只是通过一个 JS 并注释掉另一个。

因此,当我在手机 (WP8) 上时,如果我打开了“首选移动网站”,即使使用下面的代码,我也会卡在移动网站“m.smsalem.com”上。但是,如果我将其更改为“首选桌面站点”,我会先获得移动站点,可以单击“访问完整站点”,然后可以查看常规站点。

知道如果他们点击该链接,我还能做些什么来强制设置“首选移动网站”的人访问常规网站?

<?php
include 'http://smsalem.com/Mobile_Detect.php';
$detect = new Mobile_Detect();

if(strpos($_SERVER['HTTP_REFERER'], 'm.smsalem.com')){
    setcookie("noMobile", true, time()+86400);  /* expire in 1 hour */
}

if ($detect->isMobile()) {
    if(!isset($_COOKIE['noMobile'])){
        ?>
        <script type="text/javascript">
            if (screen.width <= 700) {
            window.location = "http://m.smsalem.com";
        }
    </script>
    <?php
        //header('Location: m.smsalem.com');
        //exit(0);
      }
} ?>
4

1 回答 1

0

就像 Marc B 已经提到的那样,您设置的 cookie 在您进行刷新之前不可用。如果您只将 cookie 设置为一小时,那么为什么不使用SESSION直接可用的 a 。

您的代码将如下所示:

<?php
include 'http://smsalem.com/Mobile_Detect.php';
$detect = new Mobile_Detect();

if(strpos($_SERVER['HTTP_REFERER'], 'm.smsalem.com')){
    $_SESSION["noMobile"] = true;
}

if ($detect->isMobile()) {
    if(!isset($_SESSION["noMobile"])){
        ?>
        <script type="text/javascript">
            if (screen.width <= 700) {
            window.location = "http://m.smsalem.com";
        }
    </script>
    <?php
        //header('Location: m.smsalem.com');
        //exit(0);
      }
} ?>

我个人会使用 PHPheader()来重定向用户。但是,您将无法在重定向之前检查屏幕宽度。

于 2014-03-21T09:16:55.547 回答