Click here to Skip to main content
14,487,920 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi How Do I get the Ip address of my machine in PHP code ?
Posted
Updated 18-Feb-20 1:12am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Based on similar discussion here[^], it looks like following should do:
function getip() {
    if (isSet($_SERVER)) {
        if (isSet($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } elseif (isSet($_SERVER["HTTP_CLIENT_IP"])) {
            $realip = $_SERVER["HTTP_CLIENT_IP"];
        } else {
             $realip = $_SERVER["REMOTE_ADDR"];
        }
    } else {
         if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
              $realip = getenv( 'HTTP_X_FORWARDED_FOR' );
         } elseif ( getenv( 'HTTP_CLIENT_IP' ) ) {
              $realip = getenv( 'HTTP_CLIENT_IP' );
         } else {
              $realip = getenv( 'REMOTE_ADDR' );
         }
    }
    return $realip;
}

Another helpful links: How to determine or retrieve visitor’s IP address using PHP[^] & How to get the my system's IP address[^]

Try
   
Comments
Member 7999536 6-Sep-12 3:04am
   
Sandeep, Thank you ,
It worked
Sandeep Mewara 6-Sep-12 7:53am
   
Welcome.
Espen Harlinn 6-Sep-12 10:10am
   
5'ed!
Sandeep Mewara 6-Sep-12 10:51am
   
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100