【问题标题】:Geo targeting content using php mysql使用 php mysql 的地理定位内容
【发布时间】:2012-06-30 11:09:56
【问题描述】:

我有一个网站,我只想在印度展示一张图片,而对于世界其他地方,我将展示一些其他图片。这意味着,在印度,我只想显示印度的内容,而其余国家/地区将显示一些其他内容。有人可以在这方面帮助我吗?

【问题讨论】:

    标签: php mysql ip-geolocation


    【解决方案1】:

    您需要获取访问用户的 IP 地址,并追踪他的位置,根据他的国家/地区,您可以显示自定义图片。

    一些相关问题:

    Getting the location from an IP address

    https://stackoverflow.com/questions/283016/know-a-good-ip-address-geolocation-service#364706

    这是一个通过 IP 地址检测用户位置的代码 sn-p (虽然结果不太准确)

    function detect_city($ip) {
    
            $default = 'UNKNOWN';
    
            if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')
                $ip = '8.8.8.8';
    
            $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
    
            $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);
            $ch = curl_init();
    
            $curl_opt = array(
                CURLOPT_FOLLOWLOCATION  => 1,
                CURLOPT_HEADER      => 0,
                CURLOPT_RETURNTRANSFER  => 1,
                CURLOPT_USERAGENT   => $curlopt_useragent,
                CURLOPT_URL       => $url,
                CURLOPT_TIMEOUT         => 1,
                CURLOPT_REFERER         => 'http://' . $_SERVER['HTTP_HOST'],
            );
    
            curl_setopt_array($ch, $curl_opt);
    
            $content = curl_exec($ch);
    
            if (!is_null($curl_info)) {
                $curl_info = curl_getinfo($ch);
            }
    
            curl_close($ch);
    
            if ( preg_match('{<li>City : ([^<]*)</li>}i', $content, $regs) )  {
                $city = $regs[1];
            }
            if ( preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs) )  {
                $state = $regs[1];
            }
    
            if( $city!='' && $state!='' ){
              $location = $city . ', ' . $state;
              return $location;
            }else{
              return $default; 
            }
    
    }
    

    【讨论】:

    • 他当然还需要构建自己的内容交付结构,以便为正确的国家/地区挑选正确的内容。 IP 地理定位还不够。
    猜你喜欢
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    相关资源
    最近更新 更多