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 をテストしています。したがって、常に false と評価されます。これが、モバイル ユーザーがモバイル サイトにリダイレクトされる理由です。

あなたのコードでは、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 に答える