Home > PHP > PHP Code > How to get the real IP address using php?

How to get the real IP address using php?

This post explains about how to get real IP address using PHP code.

How to get the real IP address using PHP by Anil Kumar Panigrahi

How to get the real IP address using PHP by Anil Kumar Panigrahi

The code will get the real IP address

 

 

<?php
function getRealIpAddress()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']))
//check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
//to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
echo getRealIpAddress(); // display the real IP address
?>

it will be useful.

You may also like
Website uptime monitoring using PHP script
Understanding the basic need of MVC architecture
How to convert feeds to html using javascript and php
Multi-language support for web application with MySql and PHP

6 Responses

  1. Pingback : Webby Scripts How to get the real IP address using php?

  2. raksha

    Nice ! function ipCheck() {
    if (getenv(‘HTTP_CLIENT_IP’)) {
    $ip = getenv(‘HTTP_CLIENT_IP’);
    }
    elseif (getenv(‘HTTP_X_FORWARDED_FOR’)) {
    $ip = getenv(‘HTTP_X_FORWARDED_FOR’);
    }
    elseif (getenv(‘HTTP_X_FORWARDED’)) {
    $ip = getenv(‘HTTP_X_FORWARDED’);
    }
    elseif (getenv(‘HTTP_FORWARDED_FOR’)) {
    $ip = getenv(‘HTTP_FORWARDED_FOR’);
    }
    elseif (getenv(‘HTTP_FORWARDED’)) {
    $ip = getenv(‘HTTP_FORWARDED’);
    }
    else {
    $ip = $_SERVER[‘REMOTE_ADDR’];
    }
    return $ip;
    }
    echo ipCheck();

    the above code shows external ip address . Ip-details.com also used to find public ip .

  3. Thats a great tutorial ,
    I hope this function is also working good for get user real IP.

    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;
    }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.