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

时间:2016-03-31 09:49:37

标签: javascript php mysql facebook vote

我在后端工作方面真的很陌生,我正在建立一个带有投票机制的多个照片条目的Facebook应用程序。您可以每天投票一次或多次,也可以检测您的Facebook ID,这样当您投票时,数据库会检测您的Facebook 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();
    }

1 个答案:

答案 0 :(得分:1)

$ last_vote_date AND $ 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小时。

相关问题