取得IP位址:
<code>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 { 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; } echo $ip = getIP(); </code>
//新浪介面依ip查詢所在區域資訊
<code>$res0 = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip"); $res0 = json_decode($res0,true); print_r($res0); echo "<br/>"; </code>
//淘寶介面依據ip查詢所在區域資訊
<code>$res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip"); $res1 = json_decode($res1,true); print_r($res1); echo "<br/>";</code>
以上就介紹了PHP取得IP位址以及IP位址所在位置,包括了方面的內容,希望對PHP教學有興趣的朋友有所幫助。