PHP how to get the local IP address of the system

I need to get the local IP address of the computer, for example 192. * .... Is this possible with PHP?

I need the IP address of the system running the script, but I do not need the external IP address, I need the address of the local network card.

+80
php ip-address
Jul 10 '10 at 12:28
source share
16 answers

From CLI

PHP <5.3.0

$localIP = getHostByName(php_uname('n'));

PHP> = 5.3.0

$localIP = getHostByName(getHostName());

+97
Nov 05 '13 at 18:37
source share
 $_SERVER['SERVER_ADDR'] 
+55
Jul 10 '10 at 12:30
source share

This is an old post, but get it with:

 function getLocalIp() { return gethostbyname(trim(`hostname`)); } 

For example:

 die( getLocalIp() ); 

Found on another site, do not delete the trim command, because otherwise you will get the computer name.

SPECIFICATIONS (Special quotes): This works because PHP will try to run everything in between these "special quotes" (backticks) as a shell command and return the final result.

 gethostbyname(trim(`hostname`)); 

Very similar (but much more efficient) than doing:

 $exec = exec("hostname"); //the "hostname" is a valid command in both windows and linux $hostname = trim($exec); //remove any spaces before and after $ip = gethostbyname($hostname); //resolves the hostname using local hosts resolver or DNS 
+27
Sep 10 '12 at 2:20
source share

try this (if your linux server):

 $command="/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'"; $localIP = exec ($command); echo $localIP; 
+24
Aug 17 '12 at 11:56
source share

The default external IP address for the local machine can be obtained as follows:

 $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_connect($sock, "8.8.8.8", 53); socket_getsockname($sock, $name); // $name passed by reference $localAddr = $name; 

How does it work:

Since this is UDP, socket_connect does not go online. It only assigns the local address and port to the socket. This allows us to get the local address.

This solution works without network traffic, DNS, or command execution.

May not work if there is no route to 8.8.8.8 (for example, you do not have an Internet connection).

+16
Apr 13 '16 at 16:31
source share

Depends on what you mean by local:

If by local you mean the address of the server / system executing the PHP code, then there are two more questions for discussion. If PHP is being run through a web server, you can get the server address by reading $_SERVER['SERVER_ADDR'] . If PHP is launched via the command line interface, you will most likely have to execute shell-execute ipconfig (Windows) / ifconfig (* nix) and print out the grep address.

If by local you mean the remote address of the site visitor, but not the external IP address (since you specifically said 192. *), you're out of luck. The whole point of NAT routing is to hide this address. You cannot identify the local addresses of individual computers behind an IP address, but there are some tricks (user agent, possibly a MAC address) that can help distinguish if there are several computers accessing from the same IP address.

+15
Jul 10 2018-10-10T00:
source share

hostname (1) can specify an IP address: hostname --ip-address , or, as a person says, it is better to use hostname --all-ip-addresses

+10
May 16 '13 at 19:50
source share

You can try this as a regular user in the CLI on a Linux host:

 function get_local_ipv4() { $out = split(PHP_EOL,shell_exec("/sbin/ifconfig")); $local_addrs = array(); $ifname = 'unknown'; foreach($out as $str) { $matches = array(); if(preg_match('/^([a-z0-9]+)(:\d{1,2})?(\s)+Link/',$str,$matches)) { $ifname = $matches[1]; if(strlen($matches[2])>0) { $ifname .= $matches[2]; } } elseif(preg_match('/inet addr:((?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3})\s/',$str,$matches)) { $local_addrs[$ifname] = $matches[1]; } } return $local_addrs; } $addrs = get_local_ipv4(); var_export($addrs); 

Output:

 array ( 'eth0' => '192.168.1.1', 'eth0:0' => '192.168.2.1', 'lo' => '127.0.0.1', 'vboxnet0' => '192.168.56.1', ) 
+6
Oct 22 '13 at 10:41
source share
 $localIP = gethostbyname(trim(exec("hostname"))); 

I tried it on a Windows PC and it worked, and I also think it will work on Linux.

+4
May 04 '15 at 5:02
source share

I was messing with this question for server side php (works from Linux terminal)

I blew up 'ifconfig' and trimmed it to an IP address.

Here:

 $interface_to_detect = 'wlan0'; echo explode(' ',explode(':',explode('inet addr',explode($interface_to_detect,trim('ifconfig'))[1])[1])[1])[0]; 

And, of course, change "wlan0" to your desired network device.

My conclusion:

 192.168.1.5 
+1
Jan 28 '14 at 3:47
source share

If you are in a dev environment on OS X connected via Wi-Fi:

 echo exec("/sbin/ifconfig en1 | grep 'inet ' | cut -d ' ' -f2"); 
0
Aug 25 '16 at 13:33
source share

It is very simple and the above answers complicate things. You can simply get both local and public IP addresses using this method.

  <?php $publicIP = file_get_contents("http://ipecho.net/plain"); echo $publicIP; $localIp = gethostbyname(gethostname()); echo $localIp; ?> 
0
Apr 6 '19 at 5:50
source share

It is easy. You can get the host name with this simple code.

 $ip = getHostByName(getHostName()); 

Or you can also use $_SERVER['HTTP_HOST'] to get the host name.

0
Apr 11 '19 at 12:32
source share

try it

 $localIP = gethostbyname(trim('hostname')); 
-four
Jul 19 2018-12-12T00:
source share
 $_SERVER['REMOTE_ADDR'] 
  • PHP_SELF Returns the file name of the current script with the path to the root

  • SERVER_PROTOCOL Returns the name and revision of the requested protocol page.

  • REQUEST_METHOD Returns the request method used to access the page.

  • DOCUMENT_ROOT Returns the root directory under which the current script is running.

-four
Apr 20 '13 at 7:34
source share

The easiest way to get the local IP address is using $_SERVER['SERVER_ADDR']

eg. -

 $ip_address = $_SERVER['SERVER_ADDR'] 

PHP display server IP address

-four
Apr 15 '14 at 8:10
source share



All Articles