<?php
function get_ip_place(){
$ip=file_get_contents("http://fw.qq.com/ipaddress");
$ip=str_replace('"',' ',$ip);
$ip2=explode("(",$ip);
$a=substr($ip2[1],0,-2);
$b=explode(",",$a);
return $b;
}
$ip=get_ip_place();
print_r($ip);
print_r($ip[3]); //这个就是你需要的城市了
?>代码如上,是利用腾讯(http://fw.qq.com/ipaddress)来获取来访IP及所在地的,但是PHP文件放上服务器之后(http://www.sobuso.com/tb/test3.php)是获取的服务器的IP地址,而不是访客的,请问用什么方法可以显示访客,而不是服务器的地址?
function get_ip_place(){
$ip=file_get_contents("http://fw.qq.com/ipaddress");
$ip=str_replace('"',' ',$ip);
$ip2=explode("(",$ip);
$a=substr($ip2[1],0,-2);
$b=explode(",",$a);
return $b;
}
$ip=get_ip_place();
print_r($ip);
print_r($ip[3]); //这个就是你需要的城市了
?>代码如上,是利用腾讯(http://fw.qq.com/ipaddress)来获取来访IP及所在地的,但是PHP文件放上服务器之后(http://www.sobuso.com/tb/test3.php)是获取的服务器的IP地址,而不是访客的,请问用什么方法可以显示访客,而不是服务器的地址?
<script>alert(IPData[0] + IPData[1] + IPData[2] + IPData[3]);</script>不过这样再提交到服务器就多此一举了,,,如果仅仅在页面显示出来,通过JS操作即可
获得的返回值是 数组
var IPData = new Array("xxx.xxx.xxx.xxx","","xx省","xx市");所以在js里可以直接用IPData[0]显示IP地址,IPData[2]是省 IPData[3]是市
<script type="text/javascript">
window.onload=function (){
document.getElementById('ip').innerHTML = '您的IP:'+IPData['0']+' ';
document.getElementById('sheng').innerHTML = '来自:'+IPData['2']+' ';
document.getElementById('shi').innerHTML = IPData['3'];
}
</script>
<span id="ip"></span><span id="sheng"></span><span id="shi"></span>
header("Content-Type: text/html; charset=utf-8");
<script type="text/javascript" src="http://fw.qq.com/ipaddress"></script>
<script type="text/javascript">
window.onload=function (){
document.getElementById('ip').innerHTML = '您的IP:'+IPData['0']+' ';
document.getElementById('sheng').innerHTML = '来自:'+IPData['2']+' ';
document.getElementById('shi').innerHTML = IPData['3'];
}
</script>
<span id="ip"></span><span id="sheng"></span><span id="shi"></span>?>怎么放PHP里没反应呢
另外我给你个测试了一下那个接口,它的编码是gbk18030的,需要页面还有文件的中文编码都统一才能正常显示.
b.php<script type="text/javascript" src="http://fw.qq.com/ipaddress"></script>
<script type="text/javascript" src="jquery-1.2.3.pack.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
url: "a.php",
typeType: "get",
data: "ip="+IPData,
success:function(msg){
$("#out").html(msg);
}
});
})
</script>
<div id="out"></div>
后台数据处理a.php$ip = $_GET['ip'];
echo $ip ;
$arr = explode(',',$ip);
$ip = $arr[0]; //ip地址
$sheng = $arr[2];//省份
$shi = $arr[3];//市