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


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

我对后端工作很陌生,我正在构建一个带有投票机制的多张照片的Facebook应用程序。你可以每天对一个或多个条目进行一次投票,它还会检测你的Facebook ID,这样当你投票时,数据库就会检测你的脸书ID、你投票的条目号和你投票的日期。当你在当前日期投票时,会出现一个弹出窗口,上面写着"你成功投票了"。然后,当你在同一日期再次尝试时,弹出窗口的消息会发生变化,取而代之的是,它会说"明天再尝试投票"。它还显示了参赛页面上的票数。

我唯一的问题是,当你是第一次用户时,它运行良好,你可以在当前日期对所有条目进行投票,但当你第二天回来投票时,弹出窗口仍然会显示你已经投票,并且计票不会更新。

以下是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和$current_date是日期的字符串表示,将它们转换为时间,它就起作用了:

strotime($last_vote_date) < strotime($current_date)

请注意,这将始终返回true,因为你希望他们一天都不要使用它,所以它将是:

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

增加24小时。