PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > Capturing IP addresses.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Capturing IP addresses.

Réponse
 
LinkBack Outils de la discussion
Vieux 08/09/2007, 15h32   #1
Rob
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Capturing IP addresses.

Just wondering how sites capture a visitor's IP address and how this is
done in PHP?
  Réponse avec citation
Vieux 08/09/2007, 17h04   #2
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capturing IP addresses.

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
  Réponse avec citation
Vieux 09/09/2007, 15h14   #3
Aaron Saray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capturing IP addresses.

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'];
}
}
}

  Réponse avec citation
Vieux 09/09/2007, 15h16   #4
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capturing IP addresses.

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
==================
  Réponse avec citation
Vieux 09/09/2007, 22h06   #5
esoterik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capturing IP addresses.

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?

  Réponse avec citation
Vieux 09/09/2007, 22h19   #6
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capturing IP addresses.

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
==================
  Réponse avec citation
Vieux 10/09/2007, 07h12   #7
esoterik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capturing IP addresses.

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.

  Réponse avec citation
Vieux 10/09/2007, 12h52   #8
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capturing IP addresses.

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
==================
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h11.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20165 seconds with 16 queries