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

15

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.

Reimport answered 13/5, 2009 at 3:43 Comment(0)
R
10

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

https://www.php.net/gethostbyaddr

Recency answered 13/5, 2009 at 3:47 Comment(3)
Didn't get you.. How do you find the service provider using this? Please make a clarification if canQuarles
wait.. it return my ip address... this is not i wantedReimport
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.Forgiven
E
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>
Endres answered 25/3, 2011 at 5:35 Comment(7)
Don't know why this wasn't acknowledged. Works great! Thanks.Butterwort
This violates the TOS: "You may not use a script, agent, application or otherwise query this website in an automated fashion without prior written permission."Mauk
Then what is the alternate for it @JeffWinkworth. Can you please tell.London
@JeffWinkworth How can anyone know who is crawling their website? And hence what is the use of Terms of Service in this case.Bhatt
@عثمانغني 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.Inheritable
@Bhatt 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)Inheritable
Seems like whatismyipaddress have made an API for people to use "whatismyipaddress.com/api" This is much better than violating TOS. Anybody tried it yet?Ethbin
S
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.

Stanfield answered 13/5, 2009 at 4:29 Comment(2)
Is 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.Ecclesiasticus
Correction: that function did exist back in '09. (Gotta love archive.org!)Ecclesiasticus
B
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

Bulahbulawayo answered 17/10, 2016 at 23:9 Comment(4)
Using ARIN's REST API solely for the purpose of resolving the ISP name is a violation of their TOU: arin.net/resources/registry/whois/touDysarthria
@Dysarthria - 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.)Ecclesiasticus
@Ecclesiasticus 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?Dysarthria
@Dysarthria - 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". To assume that the OP is violating policies is presumptuous/arrogant/discourteous.Ecclesiasticus
C
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> 
Culinarian answered 1/9, 2011 at 9:32 Comment(0)
G
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://mcmap.net/q/794851/-how-do-i-retrieve-the-visitor-39-s-isp-through-php
 */
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();

?>
Gabey answered 26/6, 2019 at 15:8 Comment(0)
H
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 :)

Houchens answered 13/5, 2009 at 7:45 Comment(0)
L
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.

Lacombe answered 18/3, 2010 at 6:20 Comment(0)
R
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.

Radferd answered 25/11, 2014 at 18:49 Comment(0)
V
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.

Vacillation answered 16/2, 2019 at 10:4 Comment(0)
Q
0

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

Quarles answered 13/5, 2009 at 3:47 Comment(0)
E
0

go to http://whatismyip.com

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

Explode answered 13/5, 2009 at 4:44 Comment(2)
And that relates to doing it through PHP how, exactly?Bacitracin
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.Explode
L
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

Llanes answered 24/10, 2017 at 8:59 Comment(0)
E
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.

Ecclesiasticus answered 30/6, 2019 at 7:27 Comment(1)
The rate limit is only 10K per MONTH, and the price is 25 EUR/moDovecote
R
-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;
}
?>
Reorder answered 15/4, 2013 at 23:32 Comment(2)
geoip_isp_by_name() This function is currently only available to users who have bought a commercial GeoIP ISP Edition.Mcfarlin
GeoIP is a paid library, its source code is highly obfuscated to avoid distributionPontificate

© 2022 - 2024 — McMap. All rights reserved.