Capturing IP addresses.

Capturing IP addresses.

am 08.09.2007 16:32:18 von Tyno Gendo

Just wondering how sites capture a visitor's IP address and how this is
done in PHP?

Re: Capturing IP addresses.

am 08.09.2007 18:04:39 von Shion

Rob wrote:
> Just wondering how sites capture a visitor's IP address and how this is
> done in PHP?

There is a page which lists PHP global variables, these are useful to know,
http://www.php.net/manual/en/reserved.variables.php

As you see $_SERVER['REMOTE_ADDR'] will hold the remote users IP-address (if
the remote user is using a proxy, then it's the proxy IP-address you get).

So in your code you can use:

$userip=$_SERVER['REMOTE_ADDR'];

echo "This is your ip-number: {$userip}
";

//File we want to store the ip-number to
//se to have this file already.
$filename = 'storeipnumbers.txt';
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $userip) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
echo "We have stored ip-number '{$userip}' to file '{$filename}'";
} else {
echo "Sorry, we couldn't store the following ip-number: {$userip}
";
}
?>

This script displays the ip-number and stores it to a file called
storeipnumbers.txt.
--

//Aho

Re: Capturing IP addresses.

am 09.09.2007 16:14:02 von Aaron Saray

On Sep 8, 11:04 am, "J.O. Aho" wrote:
> Rob wrote:
> > Just wondering how sites capture a visitor's IP address and how this is
> > done in PHP?
>
> There is a page which lists PHP global variables, these are useful to know,http://www.php.net/manual/en/reserved.variables.php
>
> As you see $_SERVER['REMOTE_ADDR'] will hold the remote users IP-address (if
> the remote user is using a proxy, then it's the proxy IP-address you get).
>
> So in your code you can use:
>
> > $userip=$_SERVER['REMOTE_ADDR'];
>
> echo "This is your ip-number: {$userip}
";
>
> //File we want to store the ip-number to
> //se to have this file already.
> $filename = 'storeipnumbers.txt';
> if (is_writable($filename)) {
> if (!$handle = fopen($filename, 'a')) {
> echo "Cannot open file ($filename)";
> exit;
> }
> if (fwrite($handle, $userip) === FALSE) {
> echo "Cannot write to file ($filename)";
> exit;
> }
> fclose($handle);
> echo "We have stored ip-number '{$userip}' to file '{$filename}'";} else {
>
> echo "Sorry, we couldn't store the following ip-number: {$userip}
";}
>
> ?>
>
> This script displays the ip-number and stores it to a file called
> storeipnumbers.txt.
> --
>
> //Aho

Users can also have a proxied IP address. This is what I use to
figure out the IP addresses (its from a class - hence $this):

/**
* Get user ip
*
* Get user's ip and also proxy if it exists.
*/
protected function _getIP()
{

/**
* Check for http forwarding proxy
*/
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) || !
empty($_SERVER['HTTP_CLIENT_IP'])) {

/**
* REMOTE_ADDR would be the proxy IP
*/
if (!empty($_SERVER['REMOTE_ADDR'])) {
$this->_proxyIP = $_SERVER['REMOTE_ADDR'];
}

/**
* HTTP_X_FORWARDED_FOR would be the user's ip.
*/
if (empty($_SERVER['HTTP_X_FORWARDED_FOR']) &&
empty($_SERVER['HTTP_CLIENT_IP'])) {

/**
* not set, so set our user ip to our proxy's ip as
well (or all NULL even)
*/
$this->_userIP = $this->_proxyIP;
}

/**
* get our proxy ip
*/
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$this->_userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
elseif (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$this->_userIP = $_SERVER['HTTP_CLIENT_IP'];
}
}

/**
* not proxied
*/
else {

/**
* if we can detect ip
*/
if (isset($_SERVER['REMOTE_ADDR'])) {
$this->_userIP = $_SERVER['REMOTE_ADDR'];
}
}
}

Re: Capturing IP addresses.

am 09.09.2007 16:16:58 von Jerry Stuckle

Aaron Saray wrote:
> On Sep 8, 11:04 am, "J.O. Aho" wrote:
>> Rob wrote:
>>> Just wondering how sites capture a visitor's IP address and how this is
>>> done in PHP?
>> There is a page which lists PHP global variables, these are useful to know,http://www.php.net/manual/en/reserved.variables.php
>>
>> As you see $_SERVER['REMOTE_ADDR'] will hold the remote users IP-address (if
>> the remote user is using a proxy, then it's the proxy IP-address you get).
>>
>> So in your code you can use:
>>
>> >> $userip=$_SERVER['REMOTE_ADDR'];
>>
>> echo "This is your ip-number: {$userip}
";
>>
>> //File we want to store the ip-number to
>> //se to have this file already.
>> $filename = 'storeipnumbers.txt';
>> if (is_writable($filename)) {
>> if (!$handle = fopen($filename, 'a')) {
>> echo "Cannot open file ($filename)";
>> exit;
>> }
>> if (fwrite($handle, $userip) === FALSE) {
>> echo "Cannot write to file ($filename)";
>> exit;
>> }
>> fclose($handle);
>> echo "We have stored ip-number '{$userip}' to file '{$filename}'";} else {
>>
>> echo "Sorry, we couldn't store the following ip-number: {$userip}
";}
>>
>> ?>
>>
>> This script displays the ip-number and stores it to a file called
>> storeipnumbers.txt.
>> --
>>
>> //Aho
>
> Users can also have a proxied IP address. This is what I use to
> figure out the IP addresses (its from a class - hence $this):
>
> /**
> * Get user ip
> *
> * Get user's ip and also proxy if it exists.
> */
> protected function _getIP()
> {
>
> /**
> * Check for http forwarding proxy
> */
> if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) || !
> empty($_SERVER['HTTP_CLIENT_IP'])) {
>
> /**
> * REMOTE_ADDR would be the proxy IP
> */
> if (!empty($_SERVER['REMOTE_ADDR'])) {
> $this->_proxyIP = $_SERVER['REMOTE_ADDR'];
> }
>
> /**
> * HTTP_X_FORWARDED_FOR would be the user's ip.
> */
> if (empty($_SERVER['HTTP_X_FORWARDED_FOR']) &&
> empty($_SERVER['HTTP_CLIENT_IP'])) {
>
> /**
> * not set, so set our user ip to our proxy's ip as
> well (or all NULL even)
> */
> $this->_userIP = $this->_proxyIP;
> }
>
> /**
> * get our proxy ip
> */
> elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
> $this->_userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
> }
> elseif (!empty($_SERVER['HTTP_CLIENT_IP'])) {
> $this->_userIP = $_SERVER['HTTP_CLIENT_IP'];
> }
> }
>
> /**
> * not proxied
> */
> else {
>
> /**
> * if we can detect ip
> */
> if (isset($_SERVER['REMOTE_ADDR'])) {
> $this->_userIP = $_SERVER['REMOTE_ADDR'];
> }
> }
> }
>

Which only works if the proxy actually adds the forwarded tag.
Anonymous proxies do not, and there is no way of telling where they came
from.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Capturing IP addresses.

am 09.09.2007 23:06:54 von esoterik

On Sat, 08 Sep 2007 14:32:18 +0000, Rob wrote:

> Just wondering how sites capture a visitor's IP address and how this is
> done in PHP?


any ideas how the ip address is resolved to a zipcode/location without
using one of the commercial lists?

Re: Capturing IP addresses.

am 09.09.2007 23:19:23 von Jerry Stuckle

esoterik wrote:
> On Sat, 08 Sep 2007 14:32:18 +0000, Rob wrote:
>
>> Just wondering how sites capture a visitor's IP address and how this is
>> done in PHP?
>
>
> any ideas how the ip address is resolved to a zipcode/location without
> using one of the commercial lists?
>

Nope, and even then it is not accurate. For instance, all AOL users
show as coming from Virginia. And when I'm on my dialup account from
MD, it shows I'm coming from NY.

You can get a state (or, as in the case of the Washington, DC area, a
metro area) in maybe 70-90% of the cases. But the more accurate you try
to narrow it down, the less accurate your location will be.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Capturing IP addresses.

am 10.09.2007 08:12:33 von esoterik

On Sun, 09 Sep 2007 17:19:23 -0400, Jerry Stuckle wrote:

> esoterik wrote:
>> On Sat, 08 Sep 2007 14:32:18 +0000, Rob wrote:
>>
>>> Just wondering how sites capture a visitor's IP address and how this is
>>> done in PHP?
>>
>>
>> any ideas how the ip address is resolved to a zipcode/location without
>> using one of the commercial lists?
>>
>
> Nope, and even then it is not accurate. For instance, all AOL users
> show as coming from Virginia. And when I'm on my dialup account from
> MD, it shows I'm coming from NY.
>
> You can get a state (or, as in the case of the Washington, DC area, a
> metro area) in maybe 70-90% of the cases. But the more accurate you try
> to narrow it down, the less accurate your location will be.
>
>
how exactly would you narrow it down to even the metro area of where the
IP is registered to? I know visual traceroutes exist, but I'm not sure how
I could write a script to resolve IP to an 'area' short of using cURL to
strip it off someone elses website.

Re: Capturing IP addresses.

am 10.09.2007 13:52:36 von Jerry Stuckle

esoterik wrote:
> On Sun, 09 Sep 2007 17:19:23 -0400, Jerry Stuckle wrote:
>
>> esoterik wrote:
>>> On Sat, 08 Sep 2007 14:32:18 +0000, Rob wrote:
>>>
>>>> Just wondering how sites capture a visitor's IP address and how this is
>>>> done in PHP?
>>>
>>> any ideas how the ip address is resolved to a zipcode/location without
>>> using one of the commercial lists?
>>>
>> Nope, and even then it is not accurate. For instance, all AOL users
>> show as coming from Virginia. And when I'm on my dialup account from
>> MD, it shows I'm coming from NY.
>>
>> You can get a state (or, as in the case of the Washington, DC area, a
>> metro area) in maybe 70-90% of the cases. But the more accurate you try
>> to narrow it down, the less accurate your location will be.
>>
>>
> how exactly would you narrow it down to even the metro area of where the
> IP is registered to? I know visual traceroutes exist, but I'm not sure how
> I could write a script to resolve IP to an 'area' short of using cURL to
> strip it off someone elses website.
>

Tracing the route does nothing. You need an ip address to location
reference. The most accurate (still not very accurate) are available
for a charge; the free ones I've found are not very accurate.

Why do you need this, anyway?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Capturing IP addresses.

am 10.09.2007 16:29:34 von esoterik

On Mon, 10 Sep 2007 07:52:36 -0400, Jerry Stuckle wrote:

> esoterik wrote:
>> On Sun, 09 Sep 2007 17:19:23 -0400, Jerry Stuckle wrote:
>>
>>> esoterik wrote:
>>>> On Sat, 08 Sep 2007 14:32:18 +0000, Rob wrote:
>>>>
>>>>> Just wondering how sites capture a visitor's IP address and how this is
>>>>> done in PHP?
>>>>
>>>> any ideas how the ip address is resolved to a zipcode/location without
>>>> using one of the commercial lists?
>>>>
>>> Nope, and even then it is not accurate. For instance, all AOL users
>>> show as coming from Virginia. And when I'm on my dialup account from
>>> MD, it shows I'm coming from NY.
>>>
>>> You can get a state (or, as in the case of the Washington, DC area, a
>>> metro area) in maybe 70-90% of the cases. But the more accurate you try
>>> to narrow it down, the less accurate your location will be.
>>>
>>>
>> how exactly would you narrow it down to even the metro area of where the
>> IP is registered to? I know visual traceroutes exist, but I'm not sure how
>> I could write a script to resolve IP to an 'area' short of using cURL to
>> strip it off someone elses website.
>>
>
> Tracing the route does nothing. You need an ip address to location
> reference. The most accurate (still not very accurate) are available
> for a charge; the free ones I've found are not very accurate.
>
> Why do you need this, anyway?
>

Just hoping to create a crude map of users to map across the US. I was
hoping there was some way to resolve a WHOIS type database for IP
addresses etc. Im not worried about exact locations, just mapping general
regions for people.

Re: Capturing IP addresses.

am 10.09.2007 17:30:27 von Jerry Stuckle

esoterik wrote:
> On Mon, 10 Sep 2007 07:52:36 -0400, Jerry Stuckle wrote:
>
>> esoterik wrote:
>>> On Sun, 09 Sep 2007 17:19:23 -0400, Jerry Stuckle wrote:
>>>
>>>> esoterik wrote:
>>>>> On Sat, 08 Sep 2007 14:32:18 +0000, Rob wrote:
>>>>>
>>>>>> Just wondering how sites capture a visitor's IP address and how this is
>>>>>> done in PHP?
>>>>> any ideas how the ip address is resolved to a zipcode/location without
>>>>> using one of the commercial lists?
>>>>>
>>>> Nope, and even then it is not accurate. For instance, all AOL users
>>>> show as coming from Virginia. And when I'm on my dialup account from
>>>> MD, it shows I'm coming from NY.
>>>>
>>>> You can get a state (or, as in the case of the Washington, DC area, a
>>>> metro area) in maybe 70-90% of the cases. But the more accurate you try
>>>> to narrow it down, the less accurate your location will be.
>>>>
>>>>
>>> how exactly would you narrow it down to even the metro area of where the
>>> IP is registered to? I know visual traceroutes exist, but I'm not sure how
>>> I could write a script to resolve IP to an 'area' short of using cURL to
>>> strip it off someone elses website.
>>>
>> Tracing the route does nothing. You need an ip address to location
>> reference. The most accurate (still not very accurate) are available
>> for a charge; the free ones I've found are not very accurate.
>>
>> Why do you need this, anyway?
>>
>
> Just hoping to create a crude map of users to map across the US. I was
> hoping there was some way to resolve a WHOIS type database for IP
> addresses etc. Im not worried about exact locations, just mapping general
> regions for people.
>

Not easily, I'm afraid.

As I said - the IP addresses are notoriously inaccurate. All AOL users
seem to come from Virginia because that's where AOL's internet servers
are. My dialup comes from NY, even though I'm in MD, because that's
where their servers are.

My cable link is at least in the same county, but that's not necessarily
true, even here in the DC area.

Additionally, corporations with several locations might route all of
their internet traffic through a single server (or multiple servers at
one location), accessed by their intranet. So everyone in the company
would look like they're all coming from the same location.

The commercial services try to tell you their data is accurate - and it
is, as far as it can be. But in the end the accuracy is limited by the
companies and ISPs, where they put their servers, and how they route
their traffic.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Capturing IP addresses.

am 10.09.2007 17:38:32 von esoterik

On Mon, 10 Sep 2007 11:30:27 -0400, Jerry Stuckle wrote:

> esoterik wrote:
>> On Mon, 10 Sep 2007 07:52:36 -0400, Jerry Stuckle wrote:
>>
>>> esoterik wrote:
>>>> On Sun, 09 Sep 2007 17:19:23 -0400, Jerry Stuckle wrote:
>>>>
>>>>> esoterik wrote:
>>>>>> On Sat, 08 Sep 2007 14:32:18 +0000, Rob wrote:
>>>>>>
>>>>>>> Just wondering how sites capture a visitor's IP address and how this is
>>>>>>> done in PHP?
>>>>>> any ideas how the ip address is resolved to a zipcode/location without
>>>>>> using one of the commercial lists?
>>>>>>
>>>>> Nope, and even then it is not accurate. For instance, all AOL users
>>>>> show as coming from Virginia. And when I'm on my dialup account from
>>>>> MD, it shows I'm coming from NY.
>>>>>
>>>>> You can get a state (or, as in the case of the Washington, DC area, a
>>>>> metro area) in maybe 70-90% of the cases. But the more accurate you try
>>>>> to narrow it down, the less accurate your location will be.
>>>>>
>>>>>
>>>> how exactly would you narrow it down to even the metro area of where the
>>>> IP is registered to? I know visual traceroutes exist, but I'm not sure how
>>>> I could write a script to resolve IP to an 'area' short of using cURL to
>>>> strip it off someone elses website.
>>>>
>>> Tracing the route does nothing. You need an ip address to location
>>> reference. The most accurate (still not very accurate) are available
>>> for a charge; the free ones I've found are not very accurate.
>>>
>>> Why do you need this, anyway?
>>>
>>
>> Just hoping to create a crude map of users to map across the US. I was
>> hoping there was some way to resolve a WHOIS type database for IP
>> addresses etc. Im not worried about exact locations, just mapping general
>> regions for people.
>>
>
> Not easily, I'm afraid.
>
> As I said - the IP addresses are notoriously inaccurate. All AOL users
> seem to come from Virginia because that's where AOL's internet servers
> are. My dialup comes from NY, even though I'm in MD, because that's
> where their servers are.
>
> My cable link is at least in the same county, but that's not necessarily
> true, even here in the DC area.
>
> Additionally, corporations with several locations might route all of
> their internet traffic through a single server (or multiple servers at
> one location), accessed by their intranet. So everyone in the company
> would look like they're all coming from the same location.
>
> The commercial services try to tell you their data is accurate - and it
> is, as far as it can be. But in the end the accuracy is limited by the
> companies and ISPs, where they put their servers, and how they route
> their traffic.
>

very true. I'm going to just use cURL to pull results from
http://whatismyipaddress.com/staticpages/index.php/lookup-ip which seem to
be pretty accurate for my ip's (within 5 miles in canada) and give it some
time to see how accurate it can be. thanks for the info jerry.

Re: Capturing IP addresses.

am 10.09.2007 20:16:48 von Allodoxaphobia

On Sat, 08 Sep 2007 14:32:18 GMT, Rob wrote:

>Just wondering how sites capture a visitor's IP address and how this is
>done in PHP?

And, you've _never_ come across phpinfo(); ???

Re: Capturing IP addresses.

am 10.09.2007 21:27:03 von Shion

esoterik wrote:

> how exactly would you narrow it down to even the metro area of where the
> IP is registered to? I know visual traceroutes exist, but I'm not sure how
> I could write a script to resolve IP to an 'area' short of using cURL to
> strip it off someone elses website.

*Those I have seen the user who has the ip registers the location on the
server hosted by the tools developer, the location is kept until someone else
register a new location for the ip.



--

//Aho

Re: Capturing IP addresses.

am 26.09.2007 00:22:23 von esoterik

in case anyone is following this thread, thought I would follow up with
some info. Theres is a PHP (PEAR) package called php-geo which uses a
freely available database to pull results from, and it can be upgraded to
a subscription based one for increased accuracy.

cURLing existing sites providing the information is proving to be an easy
way for a small amount of results with pretty good accuracy.

Re: Capturing IP addresses.

am 26.09.2007 19:14:41 von unknown

Post removed (X-No-Archive: yes)

Re: Capturing IP addresses.

am 27.09.2007 04:06:01 von esoterik

On Wed, 26 Sep 2007 10:14:41 -0700, Tom wrote:

> On Tue, 25 Sep 2007 22:22:23 GMT, esoterik wrote...
>>
>>in case anyone is following this thread, thought I would follow up with
>>some info. Theres is a PHP (PEAR) package called php-geo which uses a
>>freely available database to pull results from, and it can be upgraded to
>>a subscription based one for increased accuracy.
>>
>>cURLing existing sites providing the information is proving to be an easy
>>way for a small amount of results with pretty good accuracy.
>>
>
> I found a free service but not sure how easy it is to integrate into your own
> web site or if they allow it. May be a good starting point.
>
> Tom

sure id love to check it out, have a link?