Jump to content

Recommended Posts

I have a user system that lets users add points to their accounts. I want to restrict this so that they can only do this once every 24 hours. I've had a mess around in SQL with the time command but I can't figure out how to do this.

 

Anyone know how I can limit their account to only 1 vote per 24 hours?

Link to comment
https://forums.phpfreaks.com/topic/206588-daily-limits/
Share on other sites

Here is how I would approach it. Could be the best way, might not be. See what other people say. But anyway,

 

Create a last_vote column INT(10) in the table.

 

When they vote,

mysql_query("UPDATE table SET last_vote = '" . (time()) . "' WHERE user = 'whatever';");

 

Now, that sets the last time they voted.

 

Handle the actual code with something like this.

 

<?php
$last_vote_time = mysql_fetch_assoc(mysql_query("SELECT last_vote FROM table WHERE username='whatever'"));
$restrict_24hours = $last_vote_time['last_vote'] + 60 * 60 * 24;
if(isset($last_vote_time['last_vote']) && $restrict_24hours > time()){
echo "Please wait 24 hours to use this function again";
} else {
echo "You can vote";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/206588-daily-limits/#findComment-1080527
Share on other sites

Make sure the last_vote is an INT(10). This is so it can receive UNIX timestamps.

You basically generate a timestamp, by requesting time(). So I'll break it down a bit more:

 

$current_time = time();
$update_time = mysql_query("UPDATE table SET last_vote = '" . $current_time . "' WHERE username = 'whatever'");

 

As for other basics, make sure you have at least established a db connection and that querying in general can work. There should be nothing wrong with updating the time, I've tested myself

Link to comment
https://forums.phpfreaks.com/topic/206588-daily-limits/#findComment-1080540
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.