Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Based on similar discussion here[^], it looks like following should do:
PHP
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
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900