PHP投票,每天每IP一票:需要代码帮助 [英] PHP voting, one vote per IP per day: code help needed

查看:215
本文介绍了PHP投票,每天每IP一票:需要代码帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个非常基本的网站上工作,该网站允许人们每24小时对每个IP地址的一个项目(游戏)进行一次投票.投票存储在名为投票的表中,其列包含投票ID,游戏ID,投票日期和IP地址.

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.

用户通过... vote.php?id =#(其中#是指导者)进入该游戏的投票页面.这是我当前的vote.php的简化版本,无法正常工作:

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();
?>

我的目标是这样工作:

从URL中获取gid,并找到与该gid相关的所有投票.如果用户之前从未投票过该gid,则允许添加新的投票.如果用户已为该投票投票 检查先前投票的日期.如果最后一次投票时间超过24小时,则允许对该次投票再次进行投票.如果最后一次投票是在24小时之内,请拒绝对该游戏进行新的投票.

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...

我看过其他投票系统,但是我无法使jQuery,AJAX等系统适应我想要的功能,因此我认为对于我的有限理解,最基本的PHP系统是最好的.我还希望仅列出过去30天的投票,而其他投票插件的数据库设计不允许这样做.不过,如果有人有更好的选择,我还是很高兴的-我知道这很笨拙.

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.

干杯!

推荐答案

请注意,对于使用相同类型的数学运算:

Be carefull, for mathematical operations using the same type :

尝试使用floatval()

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

$return = 24.0 - $diff;

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

这篇关于PHP投票,每天每IP一票:需要代码帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆