1

从移动站点重定向后,我在保持主站点显示时遇到问题。

如果检测到移动设备,它将重定向到移动站点。移动网站上有一个“主站点”链接,点击后会带您进入主站点。由于某种原因,当您单击主站点主页上的链接时,它不会停留在主站点上,它会重定向回移动站点。

我假设 cookie 没有正确存储。

<?php
@include("Mobile_Detect.php");
$detect = new Mobile_Detect();
$allow_mobile = isset($_COOKIE['mobile'])? true:false;
if (isset($_GET['mobile'])) {
   if ($_GET['mobile']=='false'){
     setcookie("mobile", "");
     $allow_mobile = false;
   } else {
     setcookie("mobile", true, time() + 31536000, "/");
     $allow_mobile = true;
   }

}


if ($allow_mobile && $detect->isMobile()){
   if (!$detect->isTablet()) {
 header("Location:http://mobilesite.mobi");
   }
}

$not_mobile_cookie = isset($_COOKIE['notmobile'])? true:false;
if (isset($_GET['mobile'])) $not_mobile_cookie = $_GET['mobile'];


if ($not_mobile_cookie==false && $detect->isMobile()){
    if (!$detect->isTablet()) {
       header("Location:http://mobile.mobi");
    }
}

?>

这可能很简单,但我无法弄清楚。

谢谢!

4

2 回答 2

1

您的问题的关键是,当您单击主站点主页上的链接时,移动设备仍会被重定向。

您的代码正在测试一个名为 ['notmobile'] 的 cookie,它似乎没有在任何地方设置。因此总是评估为假,这就是移动用户被重定向回移动站点的原因。

在您的代码中,mobileGET 变量的用途尚不清楚,但认为它允许移动设备访问主站点,我已将下面的变量重命名为allowMobile.

假设Mobile_Detect运行正常,以下代码将允许移动设备在allowMobile=trueGET 请求后停留在主站点上。这可以通过 withallowMobile=false请求取消。

@include("Mobile_Detect.php");
$detect = new Mobile_Detect();

// Do we want to allow a mobile to view the main site content?
// If there is a cookie, yes, if not no.

$allow_mobile = (isset($_COOKIE['mobile']) && $_COOKIE['mobile']) ? true : false;

// If there is a GET allowMobile string saying 'false', delete the cookie and deny access
if (isset($_GET['allowMobile']) && $_GET['allowMobile']=='false') {
     // Delete a cookie if one exists
     setcookie("mobile", "", time()-1, "/");
     $allow_mobile = false;

} elseif (isset($_GET['allowMobile'])  {
     // if there is any other value for allowMobile, set a cookie allowing mobile access
     setcookie("mobile", true, time() + 31536000, "/");
     $allow_mobile = true;
} 

// If we DO NOT allow mobile, then redirect to the mobile site

if (!$allow_mobile && $detect->isMobile() && !$detect->isTablet()){
  header("Location: http://mobilesite.mobi");
  exit();
}

// Else, display or redirect to non-mobile page here
于 2013-01-07T07:28:10.787 回答
0

如果其他人有同样的问题,这似乎可行。我不确定这是否是正确的做法,但它对我来说工作正常。

非常感谢PassKit的帮助,非常感谢!

<?php
@include("Mobile_Detect.php");
$detect = new Mobile_Detect();

$mobile_cookie = isset($_COOKIE['mobile'])? $_COOKIE['mobile'] : "";

$force_mobile = ($mobile_cookie == "true") ? true : false;

if (isset($_GET['mobile'])) {
   if ($_GET['mobile'] == 'true') { // must we force the mobile site? if ?mobile=true then FORCE THAT MOBILE
  setcookie("mobile", "true", time() + 31536000, "/");
  $force_mobile = true;
   } else { // if ?mobile=false then remove the force
 setcookie("mobile", "false");
 $force_mobile = false;
  }
}

if ($force_mobile){

   header("Location:http://mobilesite.mobi");
} else {

   if ($detect->isMobile()){

 if ($mobile_cookie == "" && !$detect->isTablet()){

   header("Location:http://mobilesite.mobi");
 }
   }
}
?>
于 2013-01-10T07:36:12.563 回答