Jeorge Philip Jeorge Philip - 1 year ago 52
Javascript Question

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


// 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)
($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);


Answer Source

$last_vote_date AND $current_date are string representations of a date, convert them to time and it works:

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:

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

to add 24 hours.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download