简体   繁体   English

如何在 Facebook 应用程序中每天限制投票功能一次?

[英]How to limit voting function in a Facebook App once per day?

I'm really new at back-end stuff and I'm building a Facebook app with multiple photo entries with voting mechanics.我对后端的东西真的很陌生,我正在构建一个 Facebook 应用程序,其中包含多个带有投票机制的照片条目。 You can vote one or multiple entries once per day and it also detects your Facebook ID so that when you vote, the database detects your Facebook ID, the entry number you voted, and the date you voted.您可以每天为一个或多个条目投票一次,它还会检测您的 Facebook ID,以便在您投票时,数据库会检测您的 Facebook ID、您投票的条目编号以及您投票的日期。 When you vote during the current date, a pop-up will appear that says "you successfully voted".当您在当前日期投票时,将出现一个弹出窗口,显示“您已成功投票”。 Then when you try again the same date, the pop-up's message will change and instead, it will say "try to vote again tomorrow".然后,当您在同一日期再次尝试时,弹出窗口的消息将更改,而是显示“明天再次尝试投票”。 It also shows the number of votes on the entry page.它还在条目页面上显示投票数。

My only problem is that when you're a first time user, it works fine, you can vote all the entries on the current date, but then when you come back the next day, when you vote, the pop-up will still say that you already voted, and the vote count will not update.我唯一的问题是,当您是第一次用户时,它工作正常,您可以对当前日期的所有条目进行投票,但是当您第二天回来投票时,弹出窗口仍然会说您已经投票,投票数不会更新。

Here's the code for the PHP page: (edited this, already solved the problem, thanks for the help)这是PHP页面的代码:(编辑了这个,已经解决了问题,感谢帮助)

date_default_timezone_set('Asia/Manila');

        // Get last vote date: fetch_date_voted returns none if user voted for first time
        $current_date = date('Y-m-d');
        $fetch_date_return = $this->fetch_date_voted($entry_id, $facebook_id, $table_prefix, $conn);
        $last_vote_date = $fetch_date_return['last_date'];


        $return['date_last_voted'] = $fetch_date_return;

        // Below is a code i got from stackoverflow - 844641242329946 (kristina id)
        /*if( $last_vote_date && ($last_vote_date == "none" || (strtotime($last_vote_date) + (60*60*24)) < strtotime($current_date))){*/
        // Below is the original code
        if( $last_vote_date == "none" || $last_vote_date < $current_date )   {
            $table_name = $table_prefix . '_voter';
            $query = $conn->query("
                INSERT INTO $table_name
                    (facebook_id, entry_id, date_voted)
                VALUES
                    ($facebook_id, $entry_id, '$current_date')
            ");
            //

            // After doing INSERT, the variable $query will return a TRUE status 
            if ( $query ) {

                // If the date fetched is TRUE, it will UPDATE
                $update = $this->update_count($entry_id, $table_prefix, $conn);

                // If update is successful, it will fetch the latest vote_count
                if($update) {

                    $fetched_data = $this->fetch_current_count($entry_id, $table_prefix, $conn);

                    if ($fetched_data['status']) {
                            $return['status']           = true;
                            $return['vote_count']       = $fetched_data['vote_count'];

                    } else {
                        $return['status'] = false;                        
                    }
                } else {
                    $return['status'] = false;
                }

            } else {

                die('Error : ('. $conn->errno .') '. $conn->error);
                $return['status']         = false;
                $return['error_response'] = $conn->error;

            }
         } else {
            $return['status'] = false;
            $return['error_response'] = 'date_invalid';

         }


        echo json_encode($return);

        //$query->close();
        $conn->close();
    }

$last_vote_date AND $current_date are string representations of a date, convert them to time and it works: $last_vote_date 和 $current_date 是日期的字符串表示形式,将它们转换为时间即可:

strotime($last_vote_date) < strotime($current_date)

Beware that this will always return true, because you want them to not use it for a day, so it would be:请注意,这将始终返回 true,因为您希望他们一天不使用它,因此将是:

 if( $last_vote_date && ($last_vote_date == "none" || (strtotime($last_vote_date) + (60*60*24)) < strtotime($current_date))){

to add 24 hours.添加 24 小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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