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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀