How to identify server IP address in PHP

Solution 1:

Like this for the server ip:

$_SERVER['SERVER_ADDR'];

and this for the port

$_SERVER['SERVER_PORT'];

Solution 2:

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 are running a stand-alone script, not running through the web server.

Solution 3:

for example:

$_SERVER['SERVER_ADDR']

when your on IIS, try:

$_SERVER['LOCAL_ADDR']

Solution 4:

I came to this page looking for a way of getting my own ip address not the one of the remote machine connecting to me.

This will not work for a windows machine.

But in case someone searches 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 I get the primary IP address of the local machine on Linux and OS X?)