Indy
Indy

Reputation: 4957

Access site every 24 hours

I'm begginer in php, and I need your help with the following problem. Let's say for example I have two pages - index.php (where user needs to do some action) and redeem.php (his generated code). Now, here’s how it should work:

Now.. I don't know how to solve the first point. I could do this based on cookies, but some 'smart' user can simply remove them :/ so this is pointless. I guess the best way to do this is mysql but I have completely no idea how to grab time of completing the task and then work with this, depending on whether the user has done his task today or not. Ok, I hope you understand all of this ;p and expect some good advice from you guys Thanks! ;-)

Upvotes: 1

Views: 211

Answers (2)

Cristian
Cristian

Reputation: 327

Instead of cookies you simply need to save the date of when this particular action will be available. So you can do something like

$expireTime = strtotime("+1 day");

With the expire time you can simply check if the current time is major or equal to the expire date:

if (time() >= $expireTime) {
    echo "Hey, you already voted!";
} else {
    header("Location: /redeem.php");
    exit;
}

Obviously i cannot be more specific without having a piece of code to start.

Upvotes: 1

000
000

Reputation: 27227

I would create a voting_records table, with a user_id column, and a vote_datetime column. To create a new record:

INSERT INTO `voting_records` (user_id, vote_datetime) VALUES (123, NOW());

To check if a user has voted in the last 24 hours:

SELECT (DATEDIFF(NOW(), max(vote_datetime)) < 24*60*60) AS already_voted FROM voting_records WHERE user_id = 123

Upvotes: 1

Related Questions