Log in

View Full Version : Resolved When to use which?



bluewalrus
01-18-2009, 08:42 PM
I saw these 2 ways to get a users ip address which one is better to use, or is there a usage for each? Thanks.
$ip=$_SERVER['REMOTE_ADDR']

$ip=@$REMOTE_ADDR

JasonDFR
01-19-2009, 07:32 AM
I'll take a stab. I tried a quick search for something like this and found a couple things, but no explanation. I may be totally wrong.

I think that $REMOTE_ADDR is an old global variable that relies on register_globals being on. This is not good, and it is very likely that register_globals is not enabled on your server. Once PHP 6 is in use, register_globals won't even exist anymore.

And the @ symbol surpresses errors. It is used in case there is a problem with the REMOTE_ADDR variable, I think...

So ultimately the answer is to not use $ip=@$REMOTE_ADDR as it will probably not work.

If someone knows more, let us know.

Twey
01-19-2009, 08:10 AM
No, you've pretty much hit it on the head.

Anything that relies on a possibly-undefined global variable has been deprecated, since it's bad coding style and introduces security risks under certain circumstances. There are a bunch of autoglobal arrays you should use instead, such as $_SERVER, $_POST, $_GET, $_SESSION, and $_COOKIE.

bluewalrus
01-19-2009, 05:26 PM
Oo alright thanks for the explanation.