How to get 32-bits IP address in php?

Discussion in 'PHP' started by was2_0, Sep 23, 2006.

  1. #1
    I think REMOTE_ADDR return a string, right?

    Can I get the 4-bytes IP address?

    Thanks!
     
    was2_0, Sep 23, 2006 IP
  2. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    First we find the four bytes of the address then, by shifting them to the left, we add to obtain the decimal IP address:

    
    <?
    	$bytes = explode(".", $_SERVER["REMOTE_ADDR"]);
    	$decimal = ($bytes[0] << 24) + ($bytes[1] << 16) + ($bytes[2] << 8) + $bytes[3];
    ?>
    
    PHP:
     
    Evoleto, Sep 23, 2006 IP
  3. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Damn, I just realised there's a built-in function for that :) ip2long:

    
    int ip2long ( string ip_address )
    
    Code (markup):
     
    Evoleto, Sep 23, 2006 IP
  4. intoex

    intoex Peon

    Messages:
    414
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    also if needed there is back to ip2long function - string long2ip ( int proper_address)
    :)
     
    intoex, Sep 24, 2006 IP