PHP | Get private IP address from a client user?
Solution 1
This will never get to work.
First, you need an HTTP server on a client to make it response to CURL.
Even if ther would be - it is not guarantted to return whatever "local IP".
Next, an HTTP server which pings it's clients back looks suspicious and most likely gets banned.
Anyway, I tried your code on a remote IP:
array(2) {
["client_public_address"]=>
string(7) "8.8.8.8"
["client_local_address"]=>
string(0) ""
}
it returned nothing
While when I tried my local router it works okay
array(2) {
["client_public_address"]=>
string(13) "XXX"
["client_local_address"]=>
string(12) "192.168.1.37"
}
It works only for your own local router, but it cannot get whatever local address from a remote system.
Frankly, it shows you just local IP address assigned to your PC.
Solution 2
The local IP addressed is not exposed to the application layer by default. You need lower level tools that can extract this information from the network packets.
Marin Sagovac
Marin Sagovac (Marin Šagovac) PHP Web developer, developing own PHP framework, PHP Security, Java Swing, C++ GUI developer (Wxworks), FreePascal GUI development, Linux platform LinkedIn profile: http://www.linkedin.com/pub/marin-%C5%A1agovac/2b/90a/903 Twitter: https://twitter.com/marinsagovac
Updated on June 04, 2022Comments
-
Marin Sagovac almost 2 years
I know question but didn't find real answer on stackoverflow. This is not
X_FORWARDED_FOR
,SERVER_NAME
orSERVER_REMOTE_ADDR
, I want get local IP address of remote client connected to my server to detect who is really on local remote network is connected.Explain this:
ISP <----> ROUTER <----> LOCAL NETWORK <----> LOCAL PC
What I want to know?
- Public IP address of connected remote client
$_SERVER["REMOTE_ADDR"]
, okay, but!... - Local IP address of connected client on public network (192.168.x.x, 10.x.x.x, 172.x.x.x)
How to solve this problem? I have answer, so I think this should be know for everyone if want to know local IP address:
You should use
CURL
andcurl_getinfo()
function. Then, point URL address anyone that you want (your main server ip or whatever), for example:<?php $ch = curl_init(); $opt = curl_setopt($ch, CURLOPT_URL, "YOUR_SOME_URL_ADDRESS"); curl_exec($ch); $response = curl_getinfo($ch); $result = array('client_public_address' => $response["primary_ip"], 'client_local_address' => $response["local_ip"] ); var_dump($result); curl_close($ch); ?>
Focus on
$response["primary_ip"]
which responses your Public address and$response["local_ip"]
which reponses local address. Now this is example:ISP <----> ROUTER <----> LOCAL NETWORK <----> LOCAL PC /\ /\ || || \/ \/ $response["primary_ip"] <----> $response["local_ip"] 213.x.x.x (for example) 192.168.1.3 (for example)
Result:
array (size=2) 'client_public_address' => string '213.xxx.xxx.xxx' (length=14) 'client_local_address' => string '192.168.1.3' (length=11)
This will NOT be giving a REAL local IP address!
Thank you.
- Public IP address of connected remote client
-
Marin Sagovac about 11 yearsYes. You are right. I have been tested on VPS server it will return
NULL
. My mistakes, sorry. -
Marin Sagovac almost 11 yearsThis isn't for getting local IP address.