Skip to main content
Version: Current

Identify server IP address in PHP

In order to obtain the IP address of the server one can use [‘SERVER_ADDR’], it returns the IP address of the server under the current script is executing.

Another method is using the [‘REMOTE_ADDR’] in the $_SERVER array. [‘REMOTE_ADDR’] is only used for getting the IP address for the local servers although the output produced will be the same as using [‘SERVER_ADDR’] for the local server IP address.

put The code below on server_ip.php

<?php
// PHP program to obtain IP address of
// the server

// Creating a variable to store the
// server address
$ip_server = $_SERVER['SERVER_ADDR'];
$ip = $_SERVER['REMOTE_ADDR'];
// Printing the stored address
echo "IP Address is: $ip", "<br>";
echo "Server IP Address is: $ip_server";
?>