2012-01-03 7 views
2

私は私たちのためにモバイルウェブサイトを作成し、モバイルサイトの場合はメインサイトmadisonstudios.comをモバイルサイトmadisonstudios.mobiにリダイレクトしますデバイス。クッキー/リダイレクトを処理するより良い方法はありますか?

また、モバイルサイトに完全なウェブサイトボタンを配置し、リファラーがモバイルサイトの場合はクッキーを設定しますが、最初のクリックでフルサイトにリダイレクトされる問題がありました。もう一度フル・ウェブサイトに行ったときにそれをクリックすると、

この問題を解決するために、$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

答えて

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

関連する問題