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
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts

    How to allow each visitor one vote per poll?

    If tldr: If you were to code a thumbs up / thumbs down feature (that doesn't require logging in), would you use cookies, sessions, create a table and store IPs, or another method (& why)?

    I have the tables: users, polls, and choices. Each are linked through ids, polls containing userid and so forth. So far the page renders the polls and respected choices by selecting from db all polls and (JOIN) choices via userid on poll table. I then set it up so that each choice is a link that uses get methods (sends name & choiceid) to increase votecount on choices table where choiceid = id. It works perfectly (which is exciting)! Then the page uses a header to forward back to the page by using the name which was sent in the $_GET. This works as well. If you're interested, here is the code:

    PHP Code:
    <?php
        
    require_once('includes/connectdb.php');
        
    $name null;
        
    $optionid null;
        
        if(isset(
    $_GET['name']))
        {
            
    $name $_GET['name'];
        }
        
        if(isset(
    $_GET['optionid']))
        {
            
    $optionid $_GET['optionid'];
        }
        
        
    $result mysql_query('UPDATE choices SET votecount = votecount + 1 WHERE id = ' $optionid $connection );
        
    $str 'Location: http://localhost/simpolls/user.php?name=' $name;
        
    header$str ) ;
    ?>
    Now any visitor can just click click click and vote as many times as they want. I'm wondering, what would be the best way to go about this? One idea I had was to create a votes table that stores the choiceid and ip address but that seems like a lot. Then I was also thinking of just using cookies or sessions and somehow storing all pollids that have been voted on.

    If this sounds confusing pretend I'm trying to code a thumbs up / thumbs down feature. Any ideas?

    As always, thanks!

    PS: I've learned SO MUCH in the past 2 or 3 weeks thanks to y'all! Project is really coming along and I'm retaining a lot of knowledge
    Last edited by KULP; 12-03-2012 at 05:19 AM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If you have users, then you don't have to bother with IP addresses-- just allow only one vote per user per poll. Store it in a table, it's no big deal. User ID, Poll ID, what their vote was. Before you allow a vote to count, check to make sure that user hasn't voted yet.

  • #3
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Quote Originally Posted by Fumigator View Post
    If you have users, then you don't have to bother with IP addresses-- just allow only one vote per user per poll. Store it in a table, it's no big deal. User ID, Poll ID, what their vote was. Before you allow a vote to count, check to make sure that user hasn't voted yet.
    I thought about doing that but I'd rather let visitors vote without having to sign up. Should have made that more clear. How would I do it without users?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Then yeah use IP address, but that will not be 100% mischief proof. Easy to get a new IP address, or just spoof the header, or use proxy servers, etc.

  • #5
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by KULP View Post
    I thought about doing that but I'd rather let visitors vote without having to sign up. Should have made that more clear. How would I do it without users?
    How about trying to get IP address and map with the vote ID?

    In the next vote it will check the IP and then store or it will echo already voted?

    PHP Code:
    $_SERVER['REMOTE_ADDR']; 
    Also you can try finding Proxy request, but not 100% again.

    PHP Code:
    Use the following 2 solutions in PHP// methode 1 = quick but does not work with anonymous proxies

        
    $proxy_headers = array(
            
    'HTTP_VIA',
            
    'HTTP_X_FORWARDED_FOR',
            
    'HTTP_FORWARDED_FOR',
            
    'HTTP_X_FORWARDED',
            
    'HTTP_FORWARDED',
            
    'HTTP_CLIENT_IP',
            
    'HTTP_FORWARDED_FOR_IP',
            
    'VIA',
            
    'X_FORWARDED_FOR',
            
    'FORWARDED_FOR',
            
    'X_FORWARDED',
            
    'FORWARDED',
            
    'CLIENT_IP',
            
    'FORWARDED_FOR_IP',
            
    'HTTP_PROXY_CONNECTION'
        
    );
        foreach(
    $proxy_headers as $x){
            if (isset(
    $_SERVER[$x])) die("You are using a proxy!");
        }
    // Methode 2 = portscan back to the origin IP at the normal proxy ports used.

        
    $ports = array(8080,80,81,1080,6588,8000,3128,553,554,4480);
        foreach(
    $ports as $port) {
             if (@
    fsockopen($_SERVER['REMOTE_ADDR'], $port$errno$errstr30)) {
                  die(
    "You are using a proxy!");
             }
         } 
    Last edited by nani_nisha06; 12-03-2012 at 10:59 AM.

  • #6
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    As long as the worst that the 'mischief' can do is mess up poll results, it will be alright (would they be able to overload my servers by sending votes?). The votes aren't crucial as winners don't get money or anything (it would just be annoying I guess).

    Something 'kind of' like this?
    PHP Code:
    if(!userHasVoted($_SERVER['REMOTE_ADDR']))
          
    vote($_SERVER['REMOTE_ADDR'], pollidchoiceid);
    else
          echo 
    "You already voted!"
    Are you saying I would make a vote table to keep track, where I store pollid, choiceid, and ip address?

    (So now I have users, polls, choices, and votes)

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Looks like that'll work... yes I would store votes in the database.


  •  

    Posting Permissions

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