zoukankan      html  css  js  c++  java
  • PHP根据IP判断地区名信息的示例代码

    <?php 
    header("Content-type: text/html; charset=utf-8"); 
    function getIP(){ 
    if (isset($_SERVER)) { 
    if (isset($_SERVER[HTTP_X_FORWARDED_FOR])) { 
    $realip = $_SERVER[HTTP_X_FORWARDED_FOR]; 
    } elseif (isset($_SERVER[HTTP_CLIENT_IP])) { 
    $realip = $_SERVER[HTTP_CLIENT_IP]; 
    } else { 
    $realip = $_SERVER[REMOTE_ADDR]; 
    } 
    } else { 
    //getenv — 获取一个环境变量的值 if (getenv(
    "HTTP_X_FORWARDED_FOR")) { $realip = getenv( "HTTP_X_FORWARDED_FOR"); } elseif (getenv("HTTP_CLIENT_IP")) { $realip = getenv("HTTP_CLIENT_IP"); } else { $realip = getenv("REMOTE_ADDR"); } } return $realip; } $ip = getIP(); // 利用新浪接口根据ip查询所在区域信息 /* $res0 = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip"); $res0 = json_decode($res0); print_r($res0); echo "<br />"; */ // 利用淘宝接口根据ip查询所在区域信息 $res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip"); $res1 = json_decode($res1); /* print_r($res1); */ 如何读取数据, 看下面。 复制代码 代码如下: $array = get_object_vars($res1);//赋值给数组 foreach($array as $value){ echo $value->region."<br />"; echo $value->city."<br />"; echo $value->ip."<br />"; } ?> //或者用对象数据访问 echo $res1->data->city;
  • 相关阅读:
    iOS7's subview trimmed if out of parent view bounds
    iOS 7 — navigationController is setting the contentInset and ContentOffset of my UIScrollView
    友盟统计-页面访问路径
    iOS-Get the NSString height in iOS 7
    php
    How to Install Laravel PHP Framework on CentOS 7 / Ubuntu 15.04
    apache-.htaccess
    ios-member center
    lamp
    mysql
  • 原文地址:https://www.cnblogs.com/buxiangxin/p/6558310.html
Copyright © 2011-2022 走看看