【问题标题】:How can redirect users just once and if they want they can come back to page without redirect again如何只重定向一次用户,如果他们愿意,他们可以返回页面而无需再次重定向
【发布时间】:2013-12-06 04:49:26
【问题描述】:

我的 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 或其他东西来做到这一点。

【问题讨论】:

    标签: php wordpress redirect cookies


    【解决方案1】:

    这样的事情应该可以工作:

    <?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 );
    }
    

    【讨论】:

    • 您好,非常感谢!每次有人使用 bg IP 访问 test.com 时,我可以这样做吗?他会被重定向到 bg.test.com,但他可以单击指向 test.com 的链接并留在那里。下次他去 test.com 时,他又被重定向了?
    • bg 用户登陆 test.com,然后他被重定向到 bg.test.com,然后他点击 en 标志并转到 test.com(en 站点)并留在那里(即完成以前的代码,谢谢你)当这个bg用户再次打开test.com时,当他再次重定向到bg.test.com时可以做到吗(只有当他点击bg版本中的en标志时,他才能进入en版本)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2012-01-11
    相关资源
    最近更新 更多