14

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:

gethostbyaddr($_SERVER['REMOTE_ADDR']);

it returns my IP address, not my host name or ISP.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105

15 Answers15

10

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>
ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Rajesh
  • 421
  • 1
  • 5
  • 20
  • 1
    Don't know why this wasn't acknowledged. Works great! Thanks. – bozdoz Apr 26 '11 at 01:46
  • 33
    This violates the [TOS](http://whatismyipaddress.com/terms-of-use): "You may not use a script, agent, application or otherwise query this website in an automated fashion without prior written permission." – Jeff Winkworth May 10 '11 at 17:10
  • Then what is the alternate for it @JeffWinkworth. Can you please tell. – عثمان غني Feb 21 '14 at 06:53
  • @JeffWinkworth How can anyone know who is crawling their website? And hence what is the use of Terms of Service in this case. – Ankit Mar 14 '16 at 21:25
  • 1
    @عثمانغني I'm not immediately aware of an alternative, but that doesn't make this "solution" any more acceptable. A TOS violation is a TOS violation. – Doktor J Mar 31 '16 at 23:21
  • @Ankit because a GET from a script looks very different from an organic user request; supporting assets (e.g. CSS, images) are not downloaded along with the page, the request headers are different, and so on. It's actually quite easy to use software like graylog to find such requests (and their originating IP, which then gets blacklisted) – Doktor J Mar 31 '16 at 23:22
  • Seems like whatismyipaddress have made an API for people to use "https://whatismyipaddress.com/api" This is much better than violating TOS. Anybody tried it yet? – Ng Sek Long Oct 22 '18 at 04:24
10

This seems to be what you're looking for, it will attempt to return the full hostname if possible:

http://us3.php.net/gethostbyaddr

Blank
  • 7,088
  • 12
  • 49
  • 69
  • Didn't get you.. How do you find the service provider using this? Please make a clarification if can – Chathuranga Chandrasekara May 13 '09 at 03:54
  • wait.. it return my ip address... this is not i wanted –  May 13 '09 at 03:59
  • This function as any kind RDNS lookup (reverse DNS), only works if the IP address has a registered RDNS record. Also remember that all domain names registered have an IP, but not all the IPs have a related domain. – backslash17 May 13 '09 at 04:28
8

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.

James Baker
  • 5,978
  • 3
  • 25
  • 28
  • Is [`gethostbyaddr`](https://www.php.net/gethostbyaddr) not considered a *"built-in function"?* Maybe the function didn't exist back in '09 but nowadays, it seems to identify the ISP based on IP, without an issue. – ashleedawg Oct 08 '19 at 08:47
  • 1
    Correction: that function *did* exist back in '09. (Gotta love [archive.org](https://web.archive.org/web/20081218234044/http://us2.php.net/gethostbyaddr)!) – ashleedawg Oct 08 '19 at 08:48
4

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

Jacob Evans
  • 323
  • 2
  • 16
  • 3
    Using ARIN's REST API solely for the purpose of resolving the ISP name is a violation of their TOU: https://www.arin.net/resources/registry/whois/tou/ – MerlinTheMagic Mar 21 '19 at 12:46
  • @MerlinTheMagic - actually, that depends on the OP's *purpose* in retrieving the information... you're just assuming it's something nefarious but there are many valid reasons to query the service, that fall within the API's TOU. Either way it's a moot point considering the question is a decade old (but thanks for policing the internet... we need more people doing that... or maybe it's *\*fewer\** that we need.) – ashleedawg Oct 08 '19 at 08:59
  • @ashleedawg Im assuming nothing about the use. ARIN outlines 8 acceptable uses and states: "...no other use of the Whois Service other than as expressly described in these Terms is implied or permitted". Which of the 8 acceptable uses do you believe OPs usecase falls under? – MerlinTheMagic Oct 08 '19 at 12:52
  • 2
    @MerlinTheMagic - I can't speak to the use case of the OP or anyone who finds this post - which is exactly my point. There's no reason to assume that the OP intends to violate policies. I can speak to my own needs, which, for example, easily falls into [*"conducting scientific research into network operations"*](https://www.arin.net/resources/registry/whois/tou/#a-use-of-the-whois-service). To assume that the OP is violating policies is presumptuous/arrogant/discourteous. – ashleedawg Jan 28 '20 at 02:57
2

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();

?>
nico
  • 1,130
  • 2
  • 12
  • 26
2

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> 
1

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.

jwenting
  • 5,505
  • 2
  • 25
  • 30
1

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.

1

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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
navjot singh
  • 143
  • 1
  • 4
  • 15
1

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 :)

schmilblick
  • 1,917
  • 1
  • 18
  • 25
0
<?php
$isp = geoip_isp_by_name('www.example.com');
if ($isp) {
    echo 'This host IP is from ISP: ' . $isp;
}
?>

(PECL geoip >= 1.0.2)

geoip_isp_by_name — Get the Internet Service Provider (ISP) name

http://php.net/manual/ru/function.geoip-isp-by-name.php

DoctorP
  • 159
  • 2
  • 10
0

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.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
0

I think you need to use some third party service (Possibly a web service) to lookup the IP and find the service provider.

Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
0

go to http://whatismyip.com

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

Eddy
  • 1,862
  • 12
  • 12
  • 1
    And that relates to doing it through PHP how, exactly? – Chad Birch May 13 '09 at 05:03
  • 2
    I don't see your better method using php Chad. You could use curl, or a whois - many options but if you don't know about arin or whois the information I provided is helpful. – Eddy May 13 '09 at 05:23
-1

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;
}
?>
  • geoip_isp_by_name() This function is currently only available to users who have bought a commercial GeoIP ISP Edition. – Ingo Aug 24 '15 at 17:36
  • GeoIP is a paid library, its source code is highly obfuscated to avoid distribution –  May 20 '16 at 23:13