I need to get the actual (not the gateway) ip address of my computer but all the solutions I have tried are returning the gateway IP address. For instance, all suggestions from this link Get the client IP address using PHP return the gateway address. Is there a way I can get the actual ip address (public) of my computer - either in php or java?
-
What kind of gateway? Do you control it? Can you trust it? – SLaks Jan 13 '14 at 03:32
-
Is this like an AWS instance? – Machavity Jan 13 '14 at 03:33
-
When you say that you need to get the IP address of "my computer", is "my computer" the machine running the PHP script, or is it the machine with the web browser accessing the server running the PHP script? – Nick Coons Jan 13 '14 at 03:33
-
When I say gateway - I am referring to the router. I don't want to return the router's IP address, rather, I would like to return the computer (eg laptop) specific IP address. Right now with the code I am using, I get the same ip address (which is the router's ip address) on 2 different laptops used within my home. – bdfios Jan 13 '14 at 03:36
-
If the server you're accessing is outside of your network, then it can't see the IP address of your laptop, only the public IP address assigned to your router. – Nick Coons Jan 13 '14 at 03:38
-
@NickCoons - no, the "my computer" is the computer I am using to access a website (web service). The website needs to get my computer's actual ip address for some kind of verification. – bdfios Jan 13 '14 at 03:39
-
Ok thanks. Since the server outside my network can only see my public IP, is there a way I can make the website (web service) uniquely identify each computer connects to it? – bdfios Jan 13 '14 at 03:43
-
@bdfios See my answer for some thoughts on how you can uniquely identify requests. – Nick Coons Jan 13 '14 at 03:53
2 Answers
java's InetAddress class might be useful.
InetAddress IP=InetAddress.getLocalHost();
System.out.println("IP is := "+IP.getHostAddress());
Output: IP is := 10.100.95.228

- 45
- 1
- 2
- 10
From the comments, it sounds like you want to create a unique identifier for each computer accessing the server. There is no one piece of information that you can get that will act as such an identifier, here's why:
IP Address The IP address that you can see is the public address, not the private address of a device behind a NAT'd router.
MAC Address The MAC address of each network card in each computer is unique, but that information is not available outside of the LAN segment.
You may be able to use a combination of pieces of information (like the IP address and user agent) to make an educated guess that a given request is coming from a unique user, but this would be a guess.. there's no way to be certain of it.
Use $_SERVER['REMOTE_ADDR']
to get the user's public address, and then $_SERVER['USER_AGENT']
to get their user agent. If the combination of these two are unique, then there's a good chance that the request is coming from a unique user.
Another way to do this would be to set a cookie that has a distant future expiration date, or a session ID. But this all depends on how long you need to keep track of the user, if you need to keep track of them after they've closed their browser and then later returned, etc. The only full-proof way is to create an account where they authenticate on your site.

- 3,682
- 1
- 19
- 21