Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Author: jwele
- * Thread: http://www.reddit.com/r/HTML/comments/1ya65y/how_do_i_disable_a_form_for_x_hours_after_it_has/
- * Date: February 19 2014
- */
- /* Make the session and set its lifespan to 1 week */
- session_start();
- ini_set('session.cookie_lifetime', 60 * 60 * 24 * 7);
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>Session Based User Timout</title>
- <meta charset="utf-8">
- </head>
- <body>
- <?php
- date_default_timezone_set('America/Los_Angeles');
- $now = new DateTime();
- /* If the user hasn't seen the resource, set a cooldown time to prevent them
- * seeing the resource again for a desired time period
- */
- if(!isset($_SESSION['cooldown'])) {
- include 'form.php';
- $cooldown_time = new DateTime();
- // The line below adds 6 hours to the cooldown time
- $cooldown_time->add(new DateInterval('PT6H'));
- $_SESSION['cooldown'] = $cooldown_time->format('m-d-Y h:i:sA');
- } else {
- $cooldown_time = DateTime::createFromFormat('m-d-Y h:i:sA', $_SESSION['cooldown']);
- /* If the user hasn't finished his timeout */
- if ($now < $cooldown_time) {
- $time_remaining = $cooldown_time->diff($now);
- echo "This resource will unlock at ". $_SESSION['cooldown'] . ".<br>";
- echo "You have " .
- $time_remaining->h . " Hours " .
- $time_remaining->i . " Minutes and " .
- $time_remaining->s . " seconds remaining.";
- }
- /* User has waited for duration of timeout, lets remove the timeout */
- elseif ($now > $cooldown_time) {
- echo "Okay you have waited long enough, lets remove that restriction...";
- unset($_SESSION['cooldown']);
- include 'form.php';
- }
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement