get the users ip address and convert it to the ip range

Discussion in 'PHP' started by jsamdirect, Jul 30, 2007.

  1. #1
    Hi,
    I am trying to get the users ip address and convert it to the ip range.

    For example, $_SERVER['REMOTE_ADDR'] = 1.1.1.1 and I want to change it to 1.1.1.0.

    So $ip_range = 1.1.1.0

    Any thoughts?
     
    jsamdirect, Jul 30, 2007 IP
  2. themole

    themole Peon

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    From your example this would make the last octet zero:

    $range = preg_replace("#([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\.[0-9]{1,3}#", "$1.0", $_SERVER[REMOTE_ADDR]);

    -the mole
     
    themole, Jul 30, 2007 IP
  3. nagasharmi

    nagasharmi Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    use javascript

    var ipaddree=1.1.1.1
    var n=ipaddree.length
    ipaddree.setChar(n-1,'0')
    now ipaddree value is 1.1.1.0
     
    nagasharmi, Jul 31, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Why should he use JavaScript? It can be tampered with and not all users have JavaScript.

    And here's a simpler regex:
    $ip_range = preg_replace('/[0-9]+$/', '0', $_SERVER['REMOTE_ADDR']);
    PHP:
     
    krt, Jul 31, 2007 IP