I need help with the 'or' operator

Discussion in 'PHP' started by jc@ukzone.com, Feb 18, 2008.

  1. #1
    I have a trap to prevent unwanted surfers getting to the script:
    if(getenv(REMOTE_ADDR) != $vars["admin ip"]) { Go away}

    I need for two ips to be able to use the script.
    Either $vars["admin ip"] OR $vars["admin ip2"]

    I have tried:
    if(getenv(REMOTE_ADDR) != $vars["admin ip"] || $vars["admin ip2"]) { Go away}
    but it doesn't work.

    Can anyone tell me how this should be coded ???

    Thanks

    John C
     
    jc@ukzone.com, Feb 18, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    if(getenv('REMOTE_ADDR') != $vars["admin ip"] || getenv('REMOTE_ADDR') != $vars["admin ip2"]) { Go away}
    
    PHP:
    Or
    
    if (!in_array(getenv('REMOTE_ADDR'), array($vars['admin ip'], $vars['admin ip2'])))
    {
        go away
    }
    
    PHP:
     
    nico_swd, Feb 18, 2008 IP
  3. Cobnut

    Cobnut Peon

    Messages:
    184
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Personally, I'd suggest Nico's second option. Create an array for the admin IPs then use in_array to test. You may only have two now but doing it this way makes it easy to expand as far as you want to go - 2 or 200 admins.
    $admins=array('address1','address2',...);
    if(!in_array(getenv('REMOTE_ADDR'),$admins)) {
    go away
    }
    PHP:
    Jon
     
    Cobnut, Feb 18, 2008 IP
  4. jc@ukzone.com

    jc@ukzone.com Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi nico_swd

    Many thanks for your very quick reply.
    I found that the first option didn't work but the second option, as recommended by Cobnut did exactly what I wanted.

    Very many thanks to both of you.

    John C
     
    jc@ukzone.com, Feb 18, 2008 IP