0

I want to make a voting section so that when the user clicks a link it adds up a number (limit to one peer ip or something like that).

For example:

Celtics: 0 (Click to vote) -----------this one is cliked

Lakers: 0 (Click to vote)


Celtics: 1

Lakers: 0


How is this possible with sessions or just php coding?

any ideas?

user000001
  • 32,226
  • 12
  • 81
  • 108
Norman
  • 79
  • 1
  • 1
  • 7
  • if you're just using PHP, there will be no seamless way to do this, it will require a pay reload. Is that OK? If not, you'll need to use javascript as well. – Prisoner Mar 14 '11 at 00:41
  • AJAX is also usually used for this sort of thing. – Jake Lee Mar 14 '11 at 00:53
  • pay reload is ok... javascript would be nice..Is there a tutorial to be used with AJAX? – Norman Mar 14 '11 at 04:35

1 Answers1

0

Sessions could be used to provide an immediate test of whether a vote has been made or not:

// if session hasn't voted
if(!$_SESSION['voted']){

    // VOTING LOGIC

    // set session to have voted
    $_SESSION['voted'] = true;

}

However, sessions alone will not ensure that a single user cannot vote again (sessions are lost immediately upon clear browser cache/cookies) Once your voting logic fires, you'll want to store the IP address captured from $_SERVER['REMOTE_ADDR'].

// if session hasn't voted
if(!$_SESSION['voted']){

    // if ip address hasn't voted
    if(!$ip_addresses[$_SESSION['REMOTE_ADDR']]){

        // VOTING LOGIC

        // set session and ip address to have voted
        $_SESSION['voted'] = true;
        $ip_addresses[$_SESSION['REMOTE_ADDR']] = true;
    }

}

IP addresses should not be retrieved in whole like this example would require. Instead you should query an external medium (file/DB) to determine if a given client has voted or not. This is just an example implementation of course, however this would be the direction I'd head for a quick script to perform the task. You'll also want to ensure you release IP addresses after a given time, to ensure that subsequent connections from identical IP addresses but different clients can vote.

It really depends on how much you want to reduce duplicated voting. Registration of users, and restrictions based on user accounts -or- voting restrictions based on email addresses which require confirmation would be considerably more reliable, however the overhead there is much more than can be described here and now by me.

Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
  • hmmm. thnx.. its nice. If I would to use the first one, what code (php or html) can actually be used to be visualized in web page? If I would to use the second one, I just call a ip field from a database so that it can store when it has been cliked? The second one is a bit more complicated, you got a nice process? – Norman Mar 14 '11 at 04:33
  • If by first and second you're referring to my code examples, they're not really interchangeable. The second code example is a more complete expansion on the first. Also, as **Jake** mentioned, for an immediate change to occur on page (given the click was successful) you would want to perform this as an AJAX transaction. – Dan Lugg Mar 14 '11 at 04:41
  • Ok. So if I use your first code with ajax it would automatically appear on page? How can I perform this AJAX transaction? With some kind of code? – Norman Mar 14 '11 at 04:47