Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The server (should) know how to resolve its own hostname to the correct IP address even if it isn't connected to the internet.</p> <p>So all you need to do is get your hostname and then resolve the hostname to the IP address. Something like this should work just fine for you:</p> <pre><code>$ip = gethostbyname(gethostname()); </code></pre> <p>Regardless of whether the hostname is registered in DNS, an /etc/hosts file, or even ActiveDirectory, as long as it is resolvable somehow, this will get you the resolved IP address.</p> <p>You likely need to plan for other options as well. So check that the above worked, if it didn't, then you need to fall back to other options:</p> <ul> <li><code>$_SERVER['HTTP_HOST']</code> contains the address that was typed into the address bar of the browser in order to access the page. If you access the page by typing (for example) <code>http://192.168.0.1/index.php</code> into the browser, <code>$_SERVER['HTTP_HOST']</code> will be <code>192.168.0.1</code>. </li> <li>If you used a DNS name to access the page, <code>gethostbyname($_SERVER['HTTP_HOST'])</code>; will turn that DNS name into an IP address.</li> <li><code>$_SERVER['SERVER_NAME']</code> contains the name that has been configured in the web server configuration as it's server name. If you going to use this, you might as well just hard code it in PHP.</li> <li><code>$_SERVER['SERVER_ADDR']</code> will contain the operating system of the server's primary IP address. This may or may not be the IP address that was used to access the page, and it may or may not be an IP address with the web server bound to it. It depends heavily on OS and server configuration. if the server has a single IP address, this is probably a safe bet, although there are situations where this may contain <code>127.0.0.1</code>.</li> </ul> <p>You might try something like:</p> <pre><code>&lt;?php $ip = gethostbyname(gethostname()); // if we didn't get an IP address if ($ip === gethostname()){ // lets see if we can get it from the // configured web server name $ip = gethostbyname($_SERVER['SERVER_NAME']); // if we still don't have an IP address if ($ip === $_SERVER['SERVER_NAME']){ // Then we default to whatever the OS // is telling us is it's primary IP address $ip = $_SERVER['SERVER_ADDR']; } } </code></pre> <p>If you are for doing command line stuff (dangerous, potential attack vector) in linux, you cand do something like:</p> <pre><code>//then all non 127.0.0.1 ips should be in the $IPS variable echo exec('/sbin/ifconfig | grep "inet addr:" | grep -v "127.0.0.1" | cut -d: -f2 | awk \'{ print $1}\'', $IPS); $ip = ''; $foundIps = count($IPS); switch($foundIps){ case 0: print "Crud, we don't have any IP addresses here!\n"; $ip = '127.0.0.1'; break; case 1: print "Perfect, we found a single usable IP address, this must be what we want!\n"; $ip = $IPS[0]; break; default: print "Oh snap, this machine has multiple IP addresses assigned, what you wanna do about that?\n" // I'm crazy, I'll just take the second one! $ip = $IPS[1]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload