|
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
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: <?PHP $userip=$_SERVER['REMOTE_ADDR']; echo "This is your ip-number: {$userip}<br>"; //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}<br>"; } ?> This script displays the ip-number and stores it to a file called storeipnumbers.txt. -- //Aho |
|
|
|
#3 (permalink) |
|
Messages: n/a
Hébergeur: |
On Sep 8, 11:04 am, "J.O. Aho" <u...@example.net> 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: > > <?PHP > $userip=$_SERVER['REMOTE_ADDR']; > > echo "This is your ip-number: {$userip}<br>"; > > //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}<br>";} > > ?> > > 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']; } } } |
|
|
|
#4 (permalink) |
|
Messages: n/a
Hébergeur: |
Aaron Saray wrote:
> On Sep 8, 11:04 am, "J.O. Aho" <u...@example.net> 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: >> >> <?PHP >> $userip=$_SERVER['REMOTE_ADDR']; >> >> echo "This is your ip-number: {$userip}<br>"; >> >> //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}<br>";} >> >> ?> >> >> 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 ================== |
|
|
|
#5 (permalink) |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#6 (permalink) |
|
Messages: n/a
Hébergeur: |
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 ================== |
|
|
|
#7 (permalink) |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 (permalink) |
|
Messages: n/a
Hébergeur: |
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 ================== |
|
![]() |
| Outils de la discussion | |
|
|