How to identify server IP address in PHP?


The server IP can be identified with the below line of code −

$_SERVER['SERVER_ADDR'];

The port can be identified using the below line of code −

$_SERVER['SERVER_PORT'];

For PHP version 5.3 and higher, the following lines of code can be used −

$host_addr= gethostname();
$ip_addr = gethostbyname($host_addr);

This can be used when a stand-alone script is being run (which is not running via the web server).

Updated on: 06-Apr-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements