ItSolutionStuff.com

How to Get User IP Address in PHP?

By Hardik Savani • May 14, 2024
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: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get IP Address in JQuery?

Read Now →

How to Get a Current Page URL in PHP?

Read Now →

How to Get the File Size in PHP?

Read Now →

How to Get php.ini File Path in Ubuntu?

Read Now →

How to Check If String is URL or Not in PHP?

Read Now →

How to Convert Array Key to Lowercase in PHP?

Read Now →

How to Convert Array Values to Lowercase in PHP?

Read Now →

How to Get IP Address in Laravel?

Read Now →

How to Add Prefix in Each Key of PHP Array?

Read Now →

How to Get Minimum Key Value of Array in PHP?

Read Now →

How to Count Number of Files in a Directory in PHP?

Read Now →

How to Find Day Name from Specific Date in PHP?

Read Now →

How to Remove Null Values from Array in PHP?

Read Now →

Which MySQL Datatype use for Store an IP Address?

Read Now →