我的代码如下:
$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,显示返回的是各地的城市
但我把这代码放在我的网页后,再叫别人打开,全部都说是显示广州,到底哪里出问题了啊?

解决方案 »

  1.   

    呵呵! 兄弟 你的php脚本是放在服务器上的。执行也是在服务器上的。所以你只要用一个异步的http请求把请求回来的数据用eval执行一下就可以了。
      

  2.   

    file_get_contents("http://fw.qq.com/ipaddress");
    是在服务端执行的,所以获取到的地址肯定是只有一个!
      

  3.   


    AJAX算吗。。这个我有点懂。。
    但麻烦你说下原理好吗
      

  4.   

    这样只能获得你的服务器ip 。 你要象js一样的引用他  。。
      

  5.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <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>
      

  6.   

    你前面的帖子已经有把这个放到页面上就能显示当前客户端的地址,但是php想获取还要另传
    或者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>
      

  7.   

    在HTM里已经是这样了,调用了PHP文件(上面代码),因为需要进行处理
    <SCRIPT src="http:///ip.php" language='JavaScript' charset='utf-8'></SCRIPT>
      

  8.   

    http:///ip.php 这个打开是什么  从源代码
      

  9.   


    $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;
      

  10.   

    这个要用异步通信来做。
    $ip=file_get_contents("http://fw.qq.com/ipaddress");
    是始终用服务器打开这个页面,并非访客访问了这个页面。
      

  11.   


    我就知道 file_get_contents("http://fw.qq.com/ipaddress")是在服务器端执行的。
      

  12.   

    来给楼主来来个我写的:其实就是用的百度和chinaz的查询,
    在线预览: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]";
    ?>
      

  13.   

    <?php
    /*
     * @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;
    }
    ?>
      

  14.   

    你放到html页面中让用户去访问,然后用ajax返回这些数据……
      

  15.   


    document.write('<a href=http://.../><strong id="boxcithead">广州站</strong></a></div>')
      

  16.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <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>