I need to get local IP of computer like 192.*.... Is this possible with PHP?
I need IP address of system running the script, but I do not need the external IP, I need his local network card address.
I need to get local IP of computer like 192.*.... Is this possible with PHP?
I need IP address of system running the script, but I do not need the external IP, I need his local network card address.
You may try this as regular user in CLI on Linux host:
Output:
I fiddled with this question for a server-side php (running from Linux terminal)
I exploded 'ifconfig' and trimmed it down to the IP address.
Here it is:
And of course change 'wlan0' to your desired network device.
My output is:
Depends what you mean by local:
If by local you mean the address of the server/system executing the PHP code, then there are still two avenues to discuss. If PHP is being run through a web server, then you can get the server address by reading
$_SERVER['SERVER_ADDR']
. If PHP is being run through a command line interface, then you would likely have to shell-executeipconfig
(Windows) /ifconfig
(*nix) and grep out the address.If by local you mean the remote address of the website visitor, but not their external IP address (since you specifically said 192.*), then you are out of luck. The whole point of NAT routing is to hide that address. You cannot identify the local addresses of individual computers behind an IP address, but there are some tricks (user agent, possibly mac address) that can help differentiate if there are multiple computers accessing from the same IP.
The default external IP address of the local machine can be obtained like this:
How it works:
Since this is UDP,
socket_connect
doesn't go out on the network. It only assigns a local address and port to the socket. This allows us to get the local address.This solution works without any network traffic, DNS, or command execution.
It may not work if there is no route to
8.8.8.8
(e.g. you have no internet connection).It is very simple and above answers are complicating things. Simply you can get both local and public ip addresses using this method.