How to determine server IP address in PHP

How to determine server IP address in PHP?

+78
php ip
Apr 27 '11 at 7:45
source share
15 answers

As for ip server:

$_SERVER['SERVER_ADDR']; 

and for the port

 $_SERVER['SERVER_PORT']; 
+135
Apr 27 2018-11-11T00:
source share

If you are using PHP version 5.3 or higher, you can do the following:

 $host= gethostname(); $ip = gethostbyname($host); 

This works well when you run a stand-alone script, and not through a web server.

+63
Jun 11 '13 at 20:50
source share

eg:

 $_SERVER['SERVER_ADDR'] 

when you are on IIS try:

 $_SERVER['LOCAL_ADDR'] 
+19
Apr 27 2018-11-11T00:
source share

None of the most polled answers will reliably return the serverโ€™s public address. Usually $_SERVER['SERVER_ADDR'] will be correct, but if you access the server via VPN, it will most likely return an internal network address rather than a public address, and even if it is not on the same network, some configurations will be empty or there will be a different meaning.

Similarly, there are scripts in which $host= gethostname(); $ip = gethostbyname($host); $host= gethostname(); $ip = gethostbyname($host); will not return the correct values โ€‹โ€‹because it relies on both DNS (internal or external records) and server hostname parameters to extrapolate the server IP address. Both of these steps are potentially malfunctioning. For example, if the hostname of the server is formatted as a domain name (i.e. HOSTNAME=yahoo.com ), then (at least in my php5.4 / Centos6 setup) gethostbyname scan right before searching for Yahoo.com address, and not local server.

Also, since gethostbyname reverts to the public DNS records of the test server with unpublished or incorrect public DNS records (for example, you access the server using localhost or IP address, or if you redefine public DNS using your local hosts ) , then you will return either without an IP address (it will simply return the host name), or, even worse, it will return the wrong address specified in the public DNS records if it exists or there is a wildcard for the domain.

Depending on the situation, you can also try the third approach by doing something like this:

 $external_ip = exec('curl http://ipecho.net/plain; echo'); 

This has its drawbacks (it depends on the specific third-party site, and there may be network settings that route outgoing connections through another host or proxy), and how gethostbyname it can be slow. I honestly don't know which approach will be the most correct, but the lesson to be taken to heart is that the specific scenarios / configurations will lead to the wrong conclusions for all of these approaches ... so if possible, make sure that the approach , use returns the expected values.

+9
Apr 19 '15 at 17:06
source share

Check the $ _ SERVER array

 echo $_SERVER['SERVER_ADDR']; 
+5
Apr 27 2018-11-11T00:
source share

Previous answers give $ _SERVER ['SERVER_ADDR']. This will not work on some IIS installations. If you want this to work in IIS, use the following:

 $server_ip = gethostbyname($_SERVER['SERVER_NAME']); 
+4
Apr 27 2018-11-11T00: 00Z
source share

If you use PHP in a bash shell, you can use:

 $server_name=exec('hostname'); 

Because $_SERVER[] SERVER_ADDR , HTTP_HOST and SERVER_NAME not set.

+4
Oct 23 '14 at 8:32
source share

I just created a simple script that will return $ _SERVER ['REMOTE_ADDR'] and $ _SERVER ['SERVER_ADDR'] in IIS, so you don't have to change every variable. Just paste this text into your php file, which is included on every page.

 /** IIS IP Check **/ if(!$_SERVER['SERVER_ADDR']){ $_SERVER['SERVER_ADDR'] = $_SERVER['LOCAL_ADDR']; } if(!$_SERVER['REMOTE_ADDR']){ $_SERVER['REMOTE_ADDR'] = $_SERVER['LOCAL_ADDR']; } 
+2
Apr 10 '13 at
source share

I came to this page looking for a way to get my own IP address, not the one that connects to the remote computer.

This will not work on a Windows machine.

But if someone is looking for what I was looking for:

 #! /usr/bin/php <?php $my_current_ip=exec("ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'"); echo $my_current_ip; 

(Shamelessly adapted from How to get the primary IP address of the local computer in Linux and OS X? )

+2
Jul 22 '16 at 13:45
source share
 $serverIP = $_SERVER["SERVER_ADDR"]; echo "Server IP is: <b>{$serverIP}</b>"; 
+1
Apr 27 2018-11-11T00:
source share

You may need to use $HTTP_SERVER_VARS['server_ADDR'] if you are not getting any of the answers and if you are using an older version of PHP

+1
Apr 27 2018-11-11T00:
source share

I found this to work for me: GetHostByName ("");

Running XAMPP v1.7.1 on Windows 7 with the Apache web server. Unfortunately, it just gives my gateway IP address.

+1
Dec 20 '12 at 21:20
source share

This is something you could use as an adaptation of the above examples without worrying about the curl installed on your server.

 <?php // create a new cURL resource $ch = curl_init (); // set URL and other appropriate options curl_setopt ($ch, CURLOPT_URL, "http://ipecho.net/plain"); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); // grab URL and pass it to the browser $ip = curl_exec ($ch); echo "The public ip for this server is: $ip"; // close cURL resource, and free up system resources curl_close ($ch); ?> 
0
Nov 18 '17 at 9:58 on
source share

Like this:

 $_SERVER['SERVER_ADDR']; 
-one
Apr 27 2018-11-11T00:
source share

Check the $ _SERVER Array

 echo $_SERVER['SERVER_ADDR']; 
-one
Apr 03 '13 at 10:05
source share



All Articles