PHP voting, one vote per IP per day: code help nee

2019-02-20 14:43发布

问题:

I am working on a very basic site which will allow people to vote once per 24 hours on an item (game) per IP address. Votes are stored in a table named votes, with columns containing vote ID, game ID, votedate, and IP address.

Users go to the vote page for that game via ...vote.php?id=# (where # is the gid). Here is a simplified version of my current vote.php, which is not working:

<?php
$gid = $_GET["id"];
$ip = $_SERVER['REMOTE_ADDR'];
require_once ('config.php');
$q = "SELECT * FROM votes WHERE (gid=$gid)";
$r = @mysqli_query($dbc, $q);

if ($r) {
while ($row = mysqli_fetch_array($r)) {

    if ($row['ip'] != $ip) { // If no IP exists, go ahead and vote.

        $q2 = "INSERT INTO votes (gid, votedate, ip) VALUES ('$gid', NOW(), '$ip' )";
        $r2 = @mysqli_query($dbc, $q2);
        if ($r2) {
                echo 'Thank you for voting';
        } else {
            echo 'There was a problem with your vote'
            }

    } elseif ($ip == $row['ip']) {
    // If IP address exists, check date of last time they voted for this game

        // Define the two dates
        $votedate = $row['votedate'];
        $now = date("Y-m-d H:i:s");
        // Calculate hours between the dates
        $diff = round((strtotime($now) - strtotime($votedate)) / (60 * 60));
        // Create variable for when user can return.
        $return = '24' - $diff;

        if ($diff > '24') { // Allow to vote once every 24 hours
            $q3 = "INSERT INTO votes (gid, votedate, ip) VALUES ('$gid', NOW(), '$ip' )";
            $r3 = @mysqli_query($dbc, $q3);

            // Display outcome of insert query:
            if ($r3) {
                echo 'Thank you for voting, please come back in 24 hours to vote again!';
            } else {
                echo 'There was a problem with your vote:' . mysqli_error($dbc) . ' in reference to query ' . $q3;
            }
        } else {
            echo 'Sorry, you\'ve already voted in past 24 hours, please come back again in ' . $return . ' hours.';
        }

    } 

} // End of while $r loop

} else {

echo 'Sorry, your query did not work.';

} // End of overall if $r ran

mysqli_close($dbc);

exit();
?>

My goal is for it to work thus:

Grab gid from URL, and find all votes associated with that gid. If the user has never voted for that gid before, then allow new vote to be added. If the user has voted for that gid check date of previous vote(s). If last vote is older than 24 hours, allow another vote to be cast for that gid. If last vote was within 24 hours, deny new vote for that game.

Love to hear your thoughts on this...


I've looked at other voting systems but I was unable to adapt the jQuery, AJAX, etc. systems to what I wanted, so I figured a really basic PHP system is best for my limited understanding. I also want to be able to list only votes which have been cast in the past 30 days, and other voting plugins' database designs didn't allow for this. Still, I'd be happy if anyone has a fancier alternative--as I know this one is clunky.

Cheers!

回答1:

Be carefull, for mathematical operations using the same type :

Try with floatval()

$diff = floatval(round((strtotime($now) - strtotime($votedate)) / (60 * 60)));

$return = 24.0 - $diff;

if ($diff > 24.0) 
{
    /* Your code */
}