Timer in PHP on registration
Get the solution ↓↓↓I want to make little timer on my registration, per user. So basically I want to make it when someone makes the account, they are put on timer for lets say 5 minutes before registering again (same user).
I've tried the below, but it doesn't seem to be working:
$now = date('Y-m-d H:i:s');
if ($stmt->num_rows > 0) {
exit('user already exists');
} else {
$expire = date('Y-m-d H:i:s',+30);
if($now>$expire) {
create new acc
} else {
exit('You need to wait.');
}
Answer
Solution:
Just add "+ 5 min to expire" docs: https://www.php.net/manual/de/function.strtotime.php
$now = date('Y-m-d H:i:s');
if ($stmt->num_rows > 0) {
exit('user already exists');
} else {
$expire = date('Y-m-d H:i:s', "+ 5 minutes");
if($now>$expire) {
create new acc
} else {
exit('You need to wait.');
}
You can basically use this code it will work
Answer
Solution:
this code snippet will not work. Both variables,$now
and$expire
, are defined on the same flow.
On time of evaluation, the expression$now>$expire
is never true.
You need store the state for the lock. One way to achieve this is a cookie that carries the date of last registration attempt.
PseudoCode
if isRegistered(user): exit("Already signed up")
else if notSet(cookie['lastAttemptAt']) : proceedWithSignUp()
else if cookie['lastAttemptAt'].plus(5, Minutes) < now() : proceedWithSignUp()
else : exit("You need to wait")
in this case the functionproceedWithSignUp
needs to take care as well to set the cookie value oflastAttemptAt
Be aware, that this can be easily by passed, by either not accepting cookies at all or clearing the cookies.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: installation failed, reverting ./composer.json and ./composer.lock to their original content
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.