2013-11-21 51 views
0

我有這個重定向在我的index.php wordpress站點 它重定向用戶與保加利亞IP到我的本地站點 可以轉換此腳本關閉已重定向一次的用戶 我希望已重定向的用戶閱讀英文版 以便能夠閱讀並且不會再被重定向。如何重定向用戶一次,如果他們想要他們可以回到沒有重定向的頁面

<?php 

    $server = 'test'; // MySQL hostname 
    $username = 'test'; // MySQL username 
    $password = 'test'; // MySQL password 
    $dbname = 'test'; // MySQL db name 


    $db = mysql_connect($server, $username, $password) or die(mysql_error()); 
     mysql_select_db($dbname) or die(mysql_error()); 

    $sql = 'SELECT 
       country 
      FROM 
       ip2nation 
      WHERE 
       ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'") 
      ORDER BY 
       ip DESC 
      LIMIT 0,1'; 

    list($country) = mysql_fetch_row(mysql_query($sql)); 

    switch ($country) { 
    case 'bg': 
     // Get the bg to a bg site 
     header('Location: http://bg.test.com'); 
     exit;  
    } 

我該如何使用cookie或其他方法來做到這一點。

回答

0

像這樣的東西應該工作:

<?php 

    [.....] 

    list($country) = mysql_fetch_row(mysql_query($sql)); 

    /* ****** NEW CODE ******* */ 
    if (isset($_COOKIE['bg_redirect']) && $_COOKIE['bg_redirect'] == 'true'){ 
     // cookie exists! user has already been sent once 
     $country = ''; // reset so it's NOT 'bg' 
    } 

    switch ($country) { 
    case 'bg': 

     /* ****** NEW CODE ******* */ 
     // set cookie 
     setcookie('bg_redirect', 'true', time()+ 86400); // set cookie for 24 hours 

     // Get the bg to a bg site 
     header('Location: http://bg.test.com'); 
     exit; 


    } 

您也可以顯示一個鏈接,訪問者誰已被重定向,重置這樣的:

<?php if (isset($_COOKIE['bg_redirect'])) { ?> 
<a href="/?reset=true">Back to BG</a> 
<?php } ?> 

,然後在最頂端您的代碼:

<?php 

if (isset($_REQUEST['reset'])){ 
    // expire cookie 
    unset($_COOKIE['bg_redirect']); 
    setcookie('bg_redirect', null, -1); 
} 
+0

嗨,非常感謝! 我可以在每次有人使用bg IP訪問test.com時將其重定向到bg.test.com,但他可以單擊指向test.com的鏈接並停留在那裏。下一次他去test.com時,他又被重定向了嗎? – user3018298

+0

bg用戶登陸test.com,他被重定向到bg.test.com 之後,他點擊en國旗並前往test.com(en網站)並待在那裏(即完成前面的代碼並感謝您的參與那麼) 當這個bg用戶再次打開test.com時,可以做到這一點,當他再次被重定向到bg.test.com時,他沒有mather(他只能在bg版本中點擊en標誌時才能訪問en版本) – user3018298

相關問題