How do I retrieve the visitor's ISP through PHP?

后端 未结 15 1444
暖寄归人
暖寄归人 2020-12-01 11:17

How do I find out the ISP provider of a person viewing a PHP page?

Is it possible to use PHP to track or reveal it?

If I use something like the following:

相关标签:
15条回答
  • 2020-12-01 11:44

    There is nothing in the HTTP headers to indicate which ISP a user is coming from, so the answer is no, there is no PHP builtin function which will tell you this. You'd have to use some sort of service or library which maps IPs to networks/ISPs.

    0 讨论(0)
  • 2020-12-01 11:44

    go to http://whatismyip.com

    this will give you your internet address. Plug that address into the database at http://arin.net/whois

    0 讨论(0)
  • 2020-12-01 11:46

    This is the proper way to find a isp from site or ip.

    <?php
    $isp = geoip_isp_by_name('www.example.com');
    if ($isp) {
        echo 'This host IP is from ISP: ' . $isp;
    }
    ?>
    
    0 讨论(0)
  • 2020-12-01 11:47

    I've attempted to correct Ram Kumar's answer but whenever I would edit their post I would be temporarily banned and my changes were ignored. (As to why, I don't know, It was my first and only edit that I've ever made on this website.)

    Since his post, his code does not work anymore due to website changes and the Administrator implementing basic bot checks (checking the headers):

    <?php
    $IP = $_SERVER['REMOTE_ADDR'];
    
    $User_Agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0';
    $Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
    $Accept_Language = 'en-US,en;q=0.5';
    $Referer = 'http://whatismyipaddress.com/';
    $Connection = 'keep-alive';
    
    $HTML = file_get_contents("http://whatismyipaddress.com/ip/$IP", false, stream_context_create(array('http' => array('method' => 'GET', 'header' => "User-Agent: $User_Agent\r\nAccept: $Accept\r\nAccept-Language: $Accept_Language\r\nReferer: $Referer\r\nConnection: $Connection\r\n\r\n"))));
    
    preg_match_all('/<th>(.*?)<\/th><td>(.*?)<\/td>/s', $HTML, $Matches, PREG_SET_ORDER);
    
    $ISP = $Matches[3][2];
    $City = $Matches[11][2];
    $State = $Matches[10][2];
    $ZIP = $Matches[15][2];
    $Country = $Matches[9][2];
    ?>
    <body>
        <table align="center">
            <tr><td>ISP :</td><td><?php echo $ISP;?></td></tr>
            <tr><td>City :</td><td><?php echo $City;?></td></tr>
            <tr><td>State :</td><td><?php echo $State;?></td></tr>
            <tr><td>Zipcode :</td><td><?php echo $ZIP;?></td></tr>
            <tr><td>Country :</td><td><?php echo $Country;?></td></tr>
        </table>
    </body>
    

    Note that just supplying a user-agent would probably suffice and the additional headers are most likely not required, I just added them to make the request look more authentic.

    0 讨论(0)
  • 2020-12-01 11:49

    You can't rely on either the IP address or the host name to know the ISP someone is using. In fact he may not use an ISP at all, or he might be logged in through a VPN connection to his place of employment, from there using another VPN or remote desktop to a hosting service halfway around the world, and connect to you from that. The ip address you'd get would be the one from either that last remote machine or from some firewall that machine is sitting behind which might be somewhere else again.

    0 讨论(0)
  • 2020-12-01 11:51

    EDIT: This method no longer works since the website it hits now blocks automatic queries (and previously this method violated the website's terms of use). There are several other good [legal!] answers below (including my alternative this this one.)


    You can get all those things from the following PHP codings.,

    <?php
      $ip=$_SERVER['REMOTE_ADDR'];
      $url=file_get_contents("http://whatismyipaddress.com/ip/$ip");
      preg_match_all('/<th>(.*?)<\/th><td>(.*?)<\/td>/s',$url,$output,PREG_SET_ORDER);
      $isp=$output[1][2];
      $city=$output[9][2];
      $state=$output[8][2];
      $zipcode=$output[12][2];
      $country=$output[7][2];
    ?>
    <body>
      <table align="center">
        <tr><td>ISP :</td><td><?php echo $isp;?></td></tr>
        <tr><td>City :</td><td><?php echo $city;?></td></tr>
        <tr><td>State :</td><td><?php echo $state;?></td></tr>
        <tr><td>Zipcode :</td><td><?php echo $zipcode;?></td></tr>
        <tr><td>Country :</td><td><?php echo $country;?></td></tr>
      </table>
    </body>
    
    0 讨论(0)
提交回复
热议问题