Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    Whats more effencient or best way?

    A while ago I asked how to exclude a certain ip from running code, and got help with this
    it works, but looking back it seems a bit more complicated then necessary, is there a
    reason for it?

    PHP Code:
    $address = ((isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0');
    if (
    $address != '0.0.0.0' && $address != '123.456.789.012') echo "Run Code"
    Whats the difference between using that statement above, or lets say something simple
    like this or am I missing something about that code above?

    PHP Code:
    if ($_SERVER['REMOTE_ADDR'] != '123.456.789.012') echo "Run Code"
    Which one would you use out of those two above?

    Sonny
    Last edited by sonny; 08-10-2012 at 08:40 PM.

  • #2
    New Coder
    Join Date
    Nov 2011
    Posts
    96
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I would use the second one

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    The second one can trigger a notice as there is no guarantee that $_SERVER will provide REMOTE_ADDR.
    The first one guarantees an $address. I'd use an in_array after extracting the address though, as that lets me add without modifying the if conditions.

  • #4
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    The second one can trigger a notice as there is no guarantee that $_SERVER will provide REMOTE_ADDR.
    The first one guarantees an $address. I'd use an in_array after extracting the address though, as that lets me add without modifying the if conditions.
    from what I read, only a server from the distant past would not provide that,
    but a isset( would not hurt, thanks for your input, I will go with #2.

    good point I will use that with array when needing to add ips in the future
    Sonny

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Um no. It's a single line configuration in httpd.conf or .htaccess to remove the REMOTE_ADDR. Will it be removed? Probably not, but that doesn't change that it may not be there and as a good code writer you shouldn't take shortcuts.

    Otherwise this may happen: http://xkcd.com/292/

  • #6
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Um no. It's a single line configuration in httpd.conf or .htaccess to remove the REMOTE_ADDR. Will it be removed? Probably not, but that doesn't change that it may not be there and as a good code writer you shouldn't take shortcuts.

    Otherwise this may happen: http://xkcd.com/292/
    You 100% right, I changed the statement to default "to run the code" and used a isset( as well.
    good points you bought up, that's why whenever I have a code question I only post around here.

    Thanks
    Sonny

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    And I actually stand corrected. Checking the UnsetEnv isn't working to unset the environment variables in PHP. It used to, but I don't know how long ago that was now either. . .
    Still, for completion sake I'd use the isset check. Don't let the raptor sneak up on you!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •