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

后端 未结 15 1470
暖寄归人
暖寄归人 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:53

    A quick alternative. (This website allows up to 50 calls per minute.)

    $json=file_get_contents("https://extreme-ip-lookup.com/json/$ip");
    extract(json_decode($json,true));
    echo "ISP: $isp ($city, $region, $country)<br>";
    

    API details at the bottom of the page.

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

    GeoIP will help you with this: http://www.maxmind.com/app/locate_my_ip

    There is a php library for accessing geoip data: http://www.maxmind.com/app/php

    Attention though, you need to put the geoip db on your machine in order to make it work, all instructions are there :)

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

    If all these answers are not useful then you can try API way.

    1.http://extreme-ip-lookup.com/json/[IP ADDRESS HERE]

    EXAMPLE: http://extreme-ip-lookup.com/json/106.192.146.13

    2.http://ip-api.com/json/[IP ADDRESS HERE]

    EXAMPLE: http://ip-api.com/json/113.14.168.85

    once it works for you don't forget to convert JSON into PHP.

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

    Why not use ARIN's REST API.

    <?php
    
    // get IP Address
    $ip=$_SERVER['REMOTE_ADDR'];
    
    // create a new cURL resource
    $ch = curl_init();
    
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, 'http://whois.arin.net/rest/ip/' . $ip);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
    
    // execute
    $returnValue = curl_exec($ch);
    
    // close cURL resource, and free up system resources
    curl_close($ch);
    
    $result = json_decode($returnValue);
    
    echo <<<END
    <pre>
    Handle: {$result->net->handle->{'$'}}
    Ref: {$result->net->ref->{'$'}}
    Name: {$result->net->name->{'$'}}
    echo "OrgRef: {$result->net->orgRef->{'@name'}}";
    </pre>
    END;
    
    // eof
    

    https://www.arin.net/resources/whoisrws/whois_api.html

    0 讨论(0)
  • 2020-12-01 12:00

    Sometimes fields change, so this is the improvement of the above post.

    <body> 
        <table align="center">
    
    <?
     $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);
     for ($q=0; $q < 25; $q++) {
        if ($output[$q][1]) {
            if (!stripos($output[$q][2],"Blacklist")) {
                echo "<tr><td>".$output[$q][1]."</td><td>".$output[$q][2]."</td></tr>";
    
            }
        }
    }
    ?> 
        </table>
    </body> 
    
    0 讨论(0)
  • 2020-12-01 12:07

    You can obtain this information from ipinfo.io or similar services.

    <?php
    
    /**
     * Get ip info from ipinfo.io
     *
     * There are other services like this, for example
     * http://extreme-ip-lookup.com/json/106.192.146.13
     * http://ip-api.com/json/113.14.168.85
     *
     * source: https://stackoverflow.com/a/54721918/3057377
     */
    function getIpInfo($ip = '') {
        $ipinfo = file_get_contents("https://ipinfo.io/" . $ip);
        $ipinfo_json = json_decode($ipinfo, true);
        return $ipinfo_json;
    }
    
    function displayIpInfo($ipinfo_json) {
        var_dump($ipinfo_json);
        echo <<<END
    <pre>
    ip      : {$ipinfo_json['ip']}
    city    : {$ipinfo_json['city']}
    region  : {$ipinfo_json['region']}
    country : {$ipinfo_json['country']}
    loc     : {$ipinfo_json['loc']}
    postal  : {$ipinfo_json['postal']}
    org     : {$ipinfo_json['org']}
    </pre>
    END;
    }
    
    function main() {
        echo("<h1>Server IP information</h1>");
        $ipinfo_json = getIpInfo();
        displayIpInfo($ipinfo_json);
    
        echo("<h1>Visitor IP information</h1>");
        $visitor_ip = $_SERVER['REMOTE_ADDR'];
        $ipinfo_json = getIpInfo($visitor_ip);
        displayIpInfo($ipinfo_json);
    }
    
    main();
    
    ?>
    
    0 讨论(0)
提交回复
热议问题