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

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

报告相同问题?