如何限制每天一次Facebook App中的投票功能?

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

我在后端方面真的很新,我正在构建一个 Facebook 应用程序,其中包含多个带有投票机制的照片条目。您每天可以对一个或多个条目投票一次,它还会检测您的 Facebook ID,以便在您投票时,数据库会检测您的 Facebook ID、您投票的条目编号以及您投票的日期。当您在当前日期投票时,将出现一个弹出窗口,显示 "you successfully voted"。然后,当您在同一日期再次尝试时,弹出消息将更改,而是显示 "try to vote again tomorrow"。在入口页面也显示投票数。

我唯一的问题是,当你是第一次使用时,它工作正常,你可以在当前日期为所有条目投票,但是当你第二天回来投票时,弹出-up 仍然会说你已经投票了,投票数不会更新。

这是 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 Whosebug - 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)

请注意,这将始终 return 正确,因为您希望他们一天不使用它,所以它会是:

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

增加 24 小时。