Retrieve local IP address in PHP

Solved
Hello,

I’d like to know how to display the local IP address and determine whether it is static or dynamic.

The context is that I’m programming an intranet app that retrieves user information and redirects based on that information.

Thank you.

4 answers

  • Votes
  • Date
  • Descending date
  1. Moderator
    Hello, To retrieve an IP there aren’t 36 solutions...
     <?php /** 
     * Retrieve the real IP address of a visitor 
     */ 
    function get_ip() { 
        // IP if shared internet
        if (isset($_SERVER['HTTP_CLIENT_IP'])) { 
            return $_SERVER['HTTP_CLIENT_IP']; 
        } 
        // IP behind a proxy
        elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
            return $_SERVER['HTTP_X_FORWARDED_FOR']; 
        } 
        // Otherwise: normal IP
        else { 
            return (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''); 
        } 
    } 
    ?> 
    but regarding whether it is dynamic or static... I don’t know any way to determine it. (apart possibly, comparing the user’s IP on each visit to see if it matches the previous one... assuming they don’t change computers to connect...) -- Best regards, Jordane
    1. okay thanks.

      But I found this function

      gethostbyname(gethostname());


      but to determine if it’s static or not, not yet.
      1. Moderator
        This is the server's IP... not the users'
      2. I understand.
    2. Trying the code you gave me, it returns this result

      ::1
      1. Moderator
        It corresponds to the IP of the machine in "local" (in IPv6)
        Equivalent to the IP: 127.0.0.1 in IPv4
        Which makes me think that you are running your script from the same PC where your web "server" is located.
      2. indeed for the tests.
    3. 9a doesn't work; I only get the IP address.
      1. Moderator
        Well yes... What else did you expect to get?