donglan8999 2014-04-06 16:06
浏览 48

PHP GeoIP循环错误

i'm using a php redirection script, it gives me a continues loop error for the default redirection this is the code

<?php
// ccr.php - country code redirect
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
$country_code = $geoplugin->countryCode;
switch($country_code) {
case 'US':
header('Location: http://www.domain.com/en');
exit;
case 'FR':
header('Location: http://www.domain.com/fr');
exit;
case 'BE':
    header('Location: http://www.domain.com/fr');
exit;


default: // exceptions
    header('Location: http://www.domain.com');
exit;
}
?>
  • 写回答

1条回答 默认 最新

  • doudu5498 2014-04-06 16:41
    关注

    The reason you're stuck in a loop is that your script is probably executed on every page an on every page-load.

    You are most likely working with sessions, so the most convenient way would be to just set a flag there that you have already checked this user:

    <?php
    
    //...
    
    // Do this only if the flag is not set
    if( ! isset($_SESSION['checkedGeoIp']) )
    {
      // Set the flag here so you don't need to set it after each case.
      // Now this will be set for all requests during the session and 
      // the above if-statement will prevent from checking and redirecting again
      $_SESSION['checkedGeoIp'] = true;
    
      // Execute the GeoIP logic...
      require_once('geoplugin.class.php');
      $geoplugin = new geoPlugin();
      $geoplugin->locate();
      $country_code = $geoplugin->countryCode;
    
      switch ($country_code)
      {
          // ...
      }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端