2013-01-07 7 views
1

모바일 사이트에서 리디렉션 한 후 메인 사이트를 계속 표시하는 데 문제가 있습니다.모바일 사이트 만 메인 사이트 홈 페이지로 리디렉션됩니다.

모바일 장치가 감지되면 모바일 사이트로 리디렉션됩니다. 클릭하면 모바일 사이트에 "메인 사이트"링크가있어서 메인 사이트로 연결됩니다. 메인 사이트 홈 페이지의 링크를 클릭 할 때 어떤 이유로 메인 사이트에 머 무르지 않고 다시 모바일 사이트로 리디렉션됩니다.

쿠키가 올바르게 저장되지 않았다고 가정합니다.

<?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']이라는 쿠키가 테스트되어 어디에도 설정되지 않은 것 같습니다. 따라서 항상 모바일 사용자가 모바일 사이트로 리디렉션되는 이유는 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

답장을 보내 주셔서 감사합니다. 그것은 어느 쪽이라도 일하는 것처럼 보이지 않는다. 아마도 괄호가 빠져 있을까요? EDIT : 편집을 보았습니다. 지금 시도해 보겠습니다. – 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"); 
} 
    } 
} 
?> 
관련 문제