...

View Full Version : Whats more effencient or best way?



sonny
08-10-2012, 09:05 PM
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?



$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?



if ($_SERVER['REMOTE_ADDR'] != '123.456.789.012') echo "Run Code";


Which one would you use out of those two above?

Sonny

paperino00
08-10-2012, 10:20 PM
I would use the second one

Fou-Lu
08-10-2012, 11:19 PM
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.

sonny
08-11-2012, 12:44 AM
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

Fou-Lu
08-11-2012, 01:16 AM
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/

sonny
08-11-2012, 01:58 AM
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

Fou-Lu
08-11-2012, 04:24 AM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum