EASYWP - SUPERSONIC - YR - $98.88
from: Namecheap Inc
100% off! Get a FREE month of Managed WordPress Hosting

Get a .COM for just $5.98!

Get user real IP address in PHP

This article is about retrieved user real IP address by using PHP script. It's very important for a website to track visitors IP address and location. You can monitor your website's traffic location using IP address. So, why late? Let's see how to get user real IP address using PHP.   At first, we will create a function for that. This function will detect real IP from the visitor. 

function getrealip()
{
if (isset($_SERVER)){
if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
if(strpos($ip,",")){
$exp_ip = explode(",",$ip);
$ip = $exp_ip[0];
}
}else if(isset($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}else{
$ip = $_SERVER["REMOTE_ADDR"];
}
}else{
if(getenv('HTTP_X_FORWARDED_FOR')){
$ip = getenv('HTTP_X_FORWARDED_FOR');
if(strpos($ip,",")){
$exp_ip=explode(",",$ip);
$ip = $exp_ip[0];
}
}else if(getenv('HTTP_CLIENT_IP')){
$ip = getenv('HTTP_CLIENT_IP');
}else {
$ip = getenv('REMOTE_ADDR');
}
}
return $ip;
}
This function is ready to detect users IP address now we need to create a full script to use this function. Let's create a file for that name as index.php.
then put this code in this script,
index.php

<?php
function getrealip()
{
if (isset($_SERVER)){
if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
if(strpos($ip,",")){
$exp_ip = explode(",",$ip);
$ip = $exp_ip[0];
}
}else if(isset($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}else{
$ip = $_SERVER["REMOTE_ADDR"];
}
}else{
if(getenv('HTTP_X_FORWARDED_FOR')){
$ip = getenv('HTTP_X_FORWARDED_FOR');
if(strpos($ip,",")){
$exp_ip=explode(",",$ip);
$ip = $exp_ip[0];
}
}else if(getenv('HTTP_CLIENT_IP')){
$ip = getenv('HTTP_CLIENT_IP');
}else {
$ip = getenv('REMOTE_ADDR');
}
}
return $ip;
}


$MyipAddress = getrealip();
echo $MyipAddress;
?>
This script is ready to use. If you execute this script on the local server like wamp or xampp then this script will show your IP 127.0.0.1 that means your localhost  IP address. When you will execute this script in the online server then this script will show your IP address which is provided by your ISP.

Normally we get client IP address using $_SERVER['REMOTE_ADDR'] but using this script we will get user real IP if any user will use proxy & multiple IP.
$_SERVER["HTTP_X_FORWARDED_FOR"] will retrieve real IP address if any user will use proxy and $_SERVER["HTTP_CLIENT_IP"] checks if multiple IP addresses exist.
This is an advance method to retrieve any client's IP address.
Download Source Code
Unlock source code with a social share.

Did you like this article? it will be appreciated if you share a coffee or burger with the author

Sent $5 to the author
Sent $10 to the author

Need Assistance?

I'm Sajjad Hossain, working on web application development since 2012. Do you need assistance on your project? or are you stuck with problems? I am available to help you.
If you want to contact with me ping me at -

WhatsApp
Skype


We use cookies on our website. To find out more about how and why they are used or opt-out, please read our Cookie Policy. By choosing "I Accept", you consent to our use of cookies. Cookie Policy
Top