How to Get User IP Address in PHP?

By Hardik Savani November 5, 2023 Category : PHP

Hello Friends,

This tutorial is focused on how to get user ip address in php. This article goes in detailed on how to get ip address in php. We will use how to get server ip address in php. We will use how to get client ip address in php. Here, Create a basic example of php get ip address from request.

Whenever you require to get current user ip address then bellow example will help you. you can find client ip address using $_SERVER variable in php. As you can see bellow example how to find use ip address.

index.php

<?php

function getClientIp() {

$ipAddress = '';

if (isset($_SERVER['HTTP_CLIENT_IP'])){

$ipAddress = $_SERVER['HTTP_CLIENT_IP'];

} else if(isset($_SERVER['REMOTE_ADDR'])){

$ipAddress = $_SERVER['REMOTE_ADDR'];

} else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){

$ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];

} else{

$ipAddress = 'UnKnown';

}

return $ipAddress;

}

$ip = getClientIp();

echo 'User Real IP Address - '.$ip;

?>

Output:

User Real IP Address - 127.0.0.0

I hope it can help you...

Tags :
Shares