2012-01-03 83 views
2

我已經爲我們組建了一個移動網站,並且將主網站madisonstudios.com重定向到移動網站madisonstudios.mobi,如果該設備是移動設備設備。有沒有更好的方式來處理cookie /重定向

我還在移動網站上放了一個完整的網站按鈕,當引用者是移動網站時,它設置了一個cookie,但是我仍然遇到了問題,仍然會在第一次點擊時將其重定向到完整網站,那麼一旦你點擊它,第二次它去了整個網站。

要解決此問題,我添加了一個變量$setcookie並將其設置爲1,以便使其跳過重定向。我的代碼如下。

我認爲這是一種混亂的方式來做到這一點,並認爲必須有一個更清潔的方式,有沒有人有一個建議,這將有助於我使用。我是否以正確的方式去做這件事?

<?php 
if($_SERVER['HTTP_REFERER'] == "http://www.madisonstudios.mobi/" || $_SERVER['HTTP_REFERER'] == "http://madisonstudios.mobi/") 
{ 
    setcookie('fromMobi', true, time()+3600*24); 
    $setcookie = 1; 
} 

if ($_COOKIE["fromMobi"] == 1 || $setcookie == 1) 
{ 

} else { 
    $uamatches = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto","webos"); 

    foreach($uamatches as $uastring){ 
    if(preg_match("/".$uastring."/i",$_SERVER["HTTP_USER_AGENT"])) 
    { 
    header('Location: http://www.madisonstudios.mobi'); 
    } 
    } 
} 
?> 
+0

把'出口();你調用'頭之後'右()'跳過執行腳本的其餘部分,並立即制定了重定向。 – Treffynnon 2012-01-03 16:55:19

回答

2

我這樣做:

<?php 

    // Use stripos() for tidiness, case-insensitivity and ignoring subdomains and paths 
    if (stripos($_SERVER['HTTP_REFERER'],'madisonstudios.mobi') !== FALSE) { 

    // We came from mobi, set the cookie that says so 
    setcookie('fromMobi', true, time()+3600*24); 

    } else if (empty($_COOKIE["fromMobi"])) { 
    // We only do this if the cookie is not set or it has a value that evaluates 
    // to FALSE - empty() does this check for us in one go 

    // Look for mobile browsers and redirect them to mobi 
    $uamatches = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto","webos"); 
    foreach ($uamatches as $uastring) { 
     if (preg_match("/".$uastring."/i",$_SERVER["HTTP_USER_AGENT"])) { 
     header('Location: http://www.madisonstudios.mobi/'); 
     // If we find one we know we can exit straight away because the user 
     // is getting redirected 
     exit; 
     } 
    } 

    } 
+0

完美,謝謝。 – BrettAdamsGA 2012-01-03 19:52:26

相關問題