2013-01-07 101 views
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"); 
    } 
} 

?> 

這可能是簡單的東西,但我看不出來弄明白。

謝謝!

+0

你追加到主站點鏈接的變量是什麼? – MrCode

+0

變量是/?mobile = false – Craig

回答

1

問題的關鍵在於,當您點擊主網站主頁上的鏈接時,移動設備仍然會重定向到

您的代碼正在測試一個名爲['notmobile']的cookie,該cookie不會在任何地方設置。因此,總是評估爲false,這就是移動用戶被重定向回移動網站的原因。

在您的代碼中,mobile GET變量的用途尚不明確,但認爲它允許移動設備訪問主站點,我已將以下變量重命名爲allowMobile

假設Mobile_Detect運行正常,下面的代碼將允許移動設備停留在allowMobile=true GET請求後的主站點上。這可以通過allowMobile=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 
+0

謝謝您的回覆。這似乎並不奏效。可能有一個缺失的括號? 編輯:剛纔看到你的編輯,現在會嘗試 – Craig

+0

這應該工作'$ allow_mobile =(isset($ _ COOKIE ['mobile'])&& $ _COOKIE ['mobile'])? true:false;' – PassKit

+0

我已經嘗試了您的建議,但現在所做的只是直接重定向回移動網站。 如果我離開''if((!$ not_mobile_cookie || $ not_mobile_cookie =='false')&& $ detect-> isMobile())'出來,它仍然只會進入主頁,沒有其他頁面 – Craig

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"); 
} 
    } 
} 
?> 
相關問題