0

Possible Duplicate:
How do I find a user's IP address with PHP?

Is there any way to determine the ip address of the computer accessing the website. Like in cmyip.com or whatismyip.com. I don't exactly need this functionality in the project that I'm going to make. But its the first idea that came up for the problem. A unique id which will distinguish machine A from machine B. Can you give me some ideas in case determining the ip isn't possible for a beginner like me.

Community
  • 1
  • 1
Wern Ancheta
  • 22,397
  • 38
  • 100
  • 139

4 Answers4

3

You will be able to track IP addresses from users in different ways... If the user is behind a proxy, you will have a different IP address...

So, as suggested before, the environment variable REMOTE_ADDR represents the IP address of your machine or a proxy. If the environment variable "HTTP_X_FORWARDED_FOR" is set, then you are behind a proxy and you might have a different one... Here's a function that returns the correct IP and proxy IP.

function getIpAddresses() {
   $ipAddresses = array();
   if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {

      $ipAddresses['proxy'] = isset($_SERVER["HTTP_CLIENT_IP"]) ? $_SERVER["HTTP_CLIENT_IP"] : $_SERVER["REMOTE_ADDR"];
      $ipAddresses['user'] = $_SERVER["HTTP_X_FORWARDED_FOR"];

   } else {
      $ipAddresses['user'] = isset($_SERVER["HTTP_CLIENT_IP"]) ? $_SERVER["HTTP_CLIENT_IP"] : $_SERVER["REMOTE_ADDR"];
   }
   return $ipAddresses;
}

Now, the result of calling this method will always give you a map with the key "user" and, if you are behind a proxy, you can get it through the key "proxy". Here's an example...

$ips = getIpAddresses();
echo "Your IP " . $ips['user'] . "<BR>";
if (isset($ips['proxy'])) {
    echo "Your proxy IP is " . $ips['proxy'] . "<BR>";
}

// Running this will give you the following:
// From another machine
Starting the IPs... 
Your IP 192.168.48.4

// From the same machine
Starting the IPs... 
Your IP 127.0.0.1

// From the same machine with a system proxy
Starting the IPs... 
Your IP 127.0.0.1
Your proxy IP is 127.0.0.1

// From another machine with a system proxy
Starting the IPs... 
Your IP 192.168.48.4
Your proxy IP is 192.168.48.5
Marcello DeSales
  • 21,361
  • 14
  • 77
  • 80
1

If you have PHP installed, you can get the remote (user) IP with:

<?php
echo $_SERVER['REMOTE_ADDR'];
?>

It's always available (except on CLI), but might be the IP of a proxy.

cweiske
  • 30,033
  • 14
  • 133
  • 194
  • tried doing this, but its my ip that I see. I access it from the psp by typing: 192.1xx.xxx.xxx. And I see the ip address which I got from cmyip.com. Is it really meant to behave this way? The psp and the laptop that I am using are both connected to the same router – Wern Ancheta Apr 30 '11 at 15:09
1

As @cweiske said, you can find the IP trough $_SERVER['REMOTE_ADDR'];, but it might be a shared ip. You're plain out of luck with dynamic (changing because of modem reset etc) ip's, but there is a workaround for shared ips: a widely used trick is to save not only the ip, but also the useragent-string (also in the $_SERVER array).

That will change if they update browsers and all, and will not be gueranteed unique, but in the end it can be close enough.

Nanne
  • 64,065
  • 16
  • 119
  • 163
  • I dispute that it's "close enough". All you need is two people on the same network with the same browser (particularly likely in educational or business networks, where machines are set up uniformly) and your system is broken. – Lightness Races in Orbit Apr 30 '11 at 17:16
  • User-agents have the tendency to be quite unique. One different plugin, setting or version will have it changed. Smaller environments can be disregarded, big coorporations with 1 shared IP will have a hard time making all software exactly the same, which they will not have. Experience learns this is a viable way to 'identify' users. Not for login purposes ofcourse, but for clicktracking etc – Nanne May 29 '11 at 12:46
  • @Nanne: That's not true at all. "One different plugin, setting" is nonsense. – Lightness Races in Orbit May 29 '11 at 14:54
  • A random, real-life example from a place that actually uses this technique quite satisfyingly to illustrate my point: `Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; GTB6.6; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; eSobiSubscriber 2.0.4.16; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; .NET CLR 3.0.30618; .NET CLR 3.5.30729; .NET4.0C)` – Nanne May 29 '11 at 15:06
  • @Nanne: One user-agent string is not enough to suggest that you can use user-agent strings as a unique identifier behind a single IP. – Lightness Races in Orbit May 29 '11 at 15:31
  • Like I said, it is not perfect, but the situations where it does not work are rare-ish. Experience tells that you get some between 85 and 95% coverage like this, which is enough for some (mind you SOME) uses. They are certainly not 100% unique, I have never claimed that (explicitly claimed that they are not even). – Nanne May 29 '11 at 15:43
0

It sounds like you're trying to identify unique visitors, have a look at this answer: Tracking unique visitors only?.

Community
  • 1
  • 1
John Carter
  • 53,924
  • 26
  • 111
  • 144