Retrieve local IP address in PHP

Solved
chenille36 Posted messages 10 Status Member -  
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   -
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

  1. jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
     
    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
  2. chenille36 Posted messages 10 Status Member
     
    okay thanks.

    But I found this function

    gethostbyname(gethostname());


    but to determine if it’s static or not, not yet.
    0
    1. jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
       
      This is the server's IP... not the users'
      0
    2. chenille36 Posted messages 10 Status Member
       
      I understand.
      0
  3. chenille36 Posted messages 10 Status Member
     
    Trying the code you gave me, it returns this result

    ::1
    0
    1. jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
       
      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.
      0
    2. chenille36 Posted messages 10 Status Member
       
      indeed for the tests.
      0
  4. chenille36 Posted messages 10 Status Member
     
    9a doesn't work; I only get the IP address.
    0
    1. jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
       
      Well yes... What else did you expect to get?
      0