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 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Post $_SERVER['REMOTE_ADDR'] possible values (i.e. null/false)?

    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.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Quote Originally Posted by broncozr View Post
    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.

  • #3
    New Coder
    Join Date
    Apr 2007
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post
    Would using getenv() be a good way to check this value? If getenv() is false, I could just assign some dummy value like mktime() ?

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

  • #4
    Regular Coder
    Join Date
    Apr 2008
    Location
    New Jersey
    Posts
    116
    Thanks
    0
    Thanked 29 Times in 29 Posts
    No, getenv() is for something else entirely. If you're concerned with verifying that the value is there, use isset(), i.e.

    PHP Code:
    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.
    Chaos
    Lost Souls: text based RPG | MUDseek: MUD gaming search | MUDfind: MUD resource sites | Discordian Quotes


  •  

    Posting Permissions

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