doufu9947 2012-11-29 13:48
浏览 28
已采纳

用于检查双语网络的PHP DoubleEcho突然停止工作

Good day,

Sorry to trouble you with that, but i ran out of options already. On my Websites, right at the top of the head section, i have a PHP code that defines (or defined) that if you connect from Czech, or Slovakia (based on IP adress check) you'll get a Czech content, rest IP adresses will get english content. This is done by defining parameters for DoubleEcho function. It worked perfectly, however suddenly it stopped. Basics of the code is alright, I can still switch content manually, but automatic selection somehow crashed. I get english content all the time. Please, i would be really glad, if you can have a look at the code bellow.Thanks a lot in advance!

Tony S.

<?php

// Language - URL check
if(isset($_GET['en']))
  $lang = 'en';
elseif (isset($_GET['cz']))
  $lang = 'cs';
// Language - IP check
else {
  $pc = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
  $ext = array_pop(explode('.', $pc));
  if($ext == 'cz' OR $ext == 'sk')
    $lang = 'cs';
  else
    $lang = 'en';
}

    // Language - Echo function
function DoubleEcho($text_cs, $text_en)
{
  global $lang;
  if($lang == 'cs')
    echo $text_cs;
  else
    echo $text_en;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $lang; ?>" lang="<?php echo $lang; ?>">
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-language" content="<?php echo $lang; ?>" />    
  • 写回答

1条回答 默认 最新

  • duanming0494 2012-11-29 13:52
    关注

    If they go through a proxy or it's connected directly to a load balancer, the IP address will not vary.

    This function will give you an idea of detecting the IP in more cases:

    function getRemoteInfo () {
        $proxy="";
        $IP = "";
        if (isSet($_SERVER)) {
            if (isSet($_SERVER["HTTP_X_FORWARDED_FOR"])) {
                $IP = $_SERVER["HTTP_X_FORWARDED_FOR"];
                $proxy  = $_SERVER["REMOTE_ADDR"];
            } elseif (isSet($_SERVER["HTTP_CLIENT_IP"])) {
                $IP = $_SERVER["HTTP_CLIENT_IP"];
            } else {
                $IP = $_SERVER["REMOTE_ADDR"];
            }
        } else {
            if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
                $IP = getenv( 'HTTP_X_FORWARDED_FOR' );
                $proxy = getenv( 'REMOTE_ADDR' );
            } elseif ( getenv( 'HTTP_CLIENT_IP' ) ) {
                $IP = getenv( 'HTTP_CLIENT_IP' );
            } else {
                $IP = getenv( 'REMOTE_ADDR' );
            }
        }
        if (strstr($IP, ',')) {
            $ips = explode(',', $IP);
            $IP = $ips[0];
        }
        $RemoteInfo[0]=$IP;
        $RemoteInfo[1]=@GetHostByAddr($IP);
        $RemoteInfo[2]=$proxy;
        return $RemoteInfo;
    }
    

    And if you want to get fancy, I wrote a fast c++ geoip daemon that works on the city level. They also have a free country-level that would work the same way.

    https://github.com/homer6/geoipd

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。