简体   繁体   中英

Sessions: Click to raise number

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM