我的代码如下:
$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);
$c=trim($b[3]);
$d=iconv("GB2312","UTF-8",$c);
$d = str_replace("市","",$d);
$city = str_replace("特别行政区","",$d);
//$city=substr($d,0,6);
$shuchu = "document.write('<strong id=\"boxcity1_head\">".$city."站</strong></div>')"; echo $shuchu;叫别人直接打开http://fw.qq.com/ipaddress,显示返回的是各地的城市
但我把这代码放在我的网页后,再叫别人打开,全部都说是显示广州,到底哪里出问题了啊?
$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);
$c=trim($b[3]);
$d=iconv("GB2312","UTF-8",$c);
$d = str_replace("市","",$d);
$city = str_replace("特别行政区","",$d);
//$city=substr($d,0,6);
$shuchu = "document.write('<strong id=\"boxcity1_head\">".$city."站</strong></div>')"; echo $shuchu;叫别人直接打开http://fw.qq.com/ipaddress,显示返回的是各地的城市
但我把这代码放在我的网页后,再叫别人打开,全部都说是显示广州,到底哪里出问题了啊?
是在服务端执行的,所以获取到的地址肯定是只有一个!
AJAX算吗。。这个我有点懂。。
但麻烦你说下原理好吗
<HTML>
<HEAD>
<script type="text/javascript" src="http://fw.qq.com/ipaddress"></script>
</HEAD> <BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
alert(IPData.join("\r\n"));
//-->
</SCRIPT>
</BODY>
</HTML>
或者http://www.ip138.com抓取。<script type=text/javascript src=http://fw.qq.com/ipaddress charset='gb2312'></script>
<script type=text/javascript>
document.write(IPData.join(' '));
</script><script type="text/javascript">
document.write(IPData[0]+"\n"); //显示IP地址
document.write(IPData[2]+"\n"); //显示省
document.write(IPData[3]+"\n"); //显示市
</script>
<SCRIPT src="http:///ip.php" language='JavaScript' charset='utf-8'></SCRIPT>
$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);
$c=trim($b[3]);
$d=iconv("GB2312","UTF-8",$c);
$d = str_replace("市","",$d);
$city = str_replace("特别行政区","",$d);
//$city=substr($d,0,6);
$shuchu = "document.write('<strong id=\"boxcity1_head\">".$city."站</strong></div>')";echo $shuchu;
$ip=file_get_contents("http://fw.qq.com/ipaddress");
是始终用服务器打开这个页面,并非访客访问了这个页面。
我就知道 file_get_contents("http://fw.qq.com/ipaddress")是在服务器端执行的。
在线预览:http://code.h2ero.cn/showaddress.php
<?php
/*
* @Date 2011-1-1
* @Author h2ero
* Email [email protected]
* Blog blog.h2ero.cn
* 查询:在线访问者IP所在地,利用百度开放平台的查询结果。
*/
$num = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
echo "查询IP为:".$num."所在地:";
//百度数据
$numurl="http://www.baidu.com/baidu?wd=".$num."&tn=monline_4_dg";
$file=file_get_contents($numurl);
$mode="/<span style=\"margin-right:10px\">(.*)<\/span> <span>(.*)<\/span> <\/div>/U";
preg_match($mode,$file,$result);
if($result[2]==NULL)
{
echo "<br>百度数据:未知";
}
else
echo "<br>百度数据:$result[2]";
//站长之家
$num="http://tool.chinaz.com/Ip/Default.aspx?IP=".$num;
$file=file_get_contents($num);
$mode="/<a href=\"javascript:showAddress\('(.*)'\);\">/i";
preg_match($mode,$file,$result);
$result[1]=iconv("utf-8","gb2312",$result[1]);
if($result[1]==NULL)
{
echo "<br>站长之家:未知";
}
else
echo "<br>站长之家:$result[1]";
?>
/*
* @Date 2011-1-1
* @Author h2ero
* Email [email protected]
* Blog blog.h2ero.cn
* 查询:在线访问者IP所在地,利用百度开放平台的查询结果。
*/
function showipaddress()
{
$num = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$ipaddress[ip]=$num;
//百度数据
$numurl="http://www.baidu.com/baidu?wd=".$num."&tn=monline_4_dg";
$file=file_get_contents($numurl);
$mode="/<span style=\"margin-right:10px\">(.*)<\/span> <span>(.*)<\/span> <\/div>/U";
preg_match($mode,$file,$result);
$ipaddress[baidu]=$result[2];
//站长之家
$num="http://tool.chinaz.com/Ip/Default.aspx?IP=".$num;
$file=file_get_contents($num);
$mode="/<a href=\"javascript:showAddress\('(.*)'\);\">/i";
preg_match($mode,$file,$result);
$result[1]=iconv("utf-8","gb2312",$result[1]);
$ipaddress[chinaz]=$result[1];
return $ipaddress;
}
?>
document.write('<a href=http://.../><strong id="boxcithead">广州站</strong></a></div>')
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script type="text/javascript" src="http://fw.qq.com/ipaddress" charset="gb2312" ></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript" charset="gb2312">
document.write(IPData[2]);
document.write(IPData[3]); $.ready(function(){
document.write(IPData[2]);
document.write(IPData[3]);
url = "你自己的接受地址(php页面)";
//在页面中你可以用$_POST 接受 region 和 city
$.post(url, { region: IPData[2], city: IPData[3] },function(){ alert("ok");}, "html");});
</script>
<title>无标题文档</title>
</head><body>
</body>
</html>