...

View Full Version : $_SERVER['REMOTE_ADDR'] possible values (i.e. null/false)?



broncozr
05-21-2009, 04:01 AM
Can $_SERVER['REMOTE_ADDR'] ever return a null or "false" value?

I'm wanting to do a poll on my site, and I want to limit votes by cookie and by ip address. I'm going to record the ip's in a MySQL database table as they vote. If I get a match on the ip (mysql_num_rows>0), then the option to vote is not offered. However, I'm concerned that $_SERVER['REMOTE_ADDR'] will return a value that will block subsequent users.

For example, user 1 shows an ip of "null," and my code inserts "null" into the database. The next user also shows an ip of "null," and my code does not allow him to vote.

Is this a problem? If so, is there a fix/hack?

Thanks.

Inigoesdr
05-21-2009, 04:04 AM
Can $_SERVER['REMOTE_ADDR'] ever return a null or "false" value?

It should always have a value unless you access the script via the CLI(command-line interface), which won't happen when loading the page through the web server.

broncozr
05-21-2009, 05:00 AM
Would using getenv() be a good way to check this value? If getenv() is false, I could just assign some dummy value like mktime() ?


if( getenv($_SERVER['REMOTE_ADDR']) )
{
$the_ip = getenv($_SERVER['REMOTE_ADDR']);
// check database
}

chaosprime
05-21-2009, 05:23 AM
No, getenv() is for something else entirely. If you're concerned with verifying that the value is there, use isset(), i.e.


if(isset($_SERVER['REMOTE_ADDR']))
check_ip_number_against_database($_SERVER['REMOTE_ADDR']);

However, Inigoesdr is entirely correct that $_SERVER['REMOTE_ADDR'] will always be set if this script is being accessed through a Web server.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum