i'm trying to create button can click only one time in 24 hours , Even if user left the page and came back, he finds it unavailable until the 24h end , I don't mind any possible way (java script , database , jquery , php). thank you all guys
i have this code html:
<form action="#" method="POST">
<div class="card1" id="card1">
<h3>book 1</h3>
<button id="roll1""name="roll1"><a href="bbb.php">read now</a></button>
</div>
<div class="card2" id="card2">
<h3>book 2</h3>
<button id="roll2" "name="roll2"> <a href="ccc.php">read now</a></button>
</div>
<div class="card3" id="card3">
<h3 >book 3</h3>
<botton id="roll3" "name="roll3""> <a href="aaa.php">read now</a></button>
</div>
</form>
I tried to do that but when user refresh the button back Enabled :
<?php
if(isset($_POST['roll'])) {
echo 'you can not click , try again later';
}
?>
<form action="aaa.php" method="POST">
<button type="submit" name="roll" <?php echo isset($_POST["roll"]) ?
"disabled" : "";?>>read now </button>
</form>
You can try this with session. If session is persisted to a DB, It will be better, because the user can delete the session on the browser.
On your index.php file (where your form is), you can have this :
<?php
session_start();
?>
<form action="aaa.php" method="POST">
<button type="submit" name="roll" value="roll"
<?php
echo isset($_SESSION["lock_timestamp_24h"]) &&
date('m/d/Y H:i:s', time()) <= date('m/d/Y H:i:s', $_SESSION["lock_timestamp_24h"]) ? 'disabled' : ''
?>
>read now
</button>
</form>
As I see in your question you post on aaa.php file. So on this file, you could do this :
<?php
session_start();
if (isset($_POST['roll'])) {
if( !isset($_SESSION["lock_timestamp_24h"]) ) {
$_SESSION["lock_timestamp_24h"] = strtotime("+ 1 day", (new DateTime())->getTimestamp());
}
}