Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP IP Address
Hi
How Do I get the Ip address of my machine in PHP code ?
Posted 5-Sep-12 6:15am

1 solution

Rate this: bad
good
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
  Permalink  
Comments
Member 7999536 at 6-Sep-12 3:04am
   
Sandeep, Thank you ,
It worked
Sandeep Mewara at 6-Sep-12 7:53am
   
Welcome.
Espen Harlinn at 6-Sep-12 10:10am
   
5'ed!
Sandeep Mewara at 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)

  Print Answers RSS
0 OriginalGriff 468
1 Sergey Alexandrovich Kryukov 323
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Peter Leow 100
0 Sergey Alexandrovich Kryukov 6,465
1 OriginalGriff 5,926
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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