Advertisement
Guest User

sessionsexample.php

a guest
Feb 19th, 2014
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.63 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Author: jwele
  4.  * Thread: http://www.reddit.com/r/HTML/comments/1ya65y/how_do_i_disable_a_form_for_x_hours_after_it_has/
  5.  * Date: February 19 2014
  6.  */
  7.     /* Make the session and set its lifespan to 1 week */
  8.     session_start();
  9.     ini_set('session.cookie_lifetime', 60 * 60 * 24 * 7);
  10. ?>
  11. <!DOCTYPE html>
  12. <html lang="en">
  13. <head>
  14. <title>Session Based User Timout</title>
  15. <meta charset="utf-8">
  16. </head>
  17. <body>
  18. <?php
  19.     date_default_timezone_set('America/Los_Angeles');
  20.     $now = new DateTime();
  21.     /* If the user hasn't seen the resource, set a cooldown time to prevent them
  22.      * seeing the resource again for a desired time period
  23.      */
  24.     if(!isset($_SESSION['cooldown'])) {
  25.         include 'form.php';
  26.         $cooldown_time = new DateTime();
  27.         // The line below adds 6 hours to the cooldown time
  28.         $cooldown_time->add(new DateInterval('PT6H'));
  29.         $_SESSION['cooldown'] = $cooldown_time->format('m-d-Y h:i:sA');
  30.     } else {
  31.         $cooldown_time = DateTime::createFromFormat('m-d-Y h:i:sA', $_SESSION['cooldown']);
  32.         /* If the user hasn't finished his timeout */
  33.         if ($now < $cooldown_time) {
  34.             $time_remaining = $cooldown_time->diff($now);
  35.             echo "This resource will unlock at ". $_SESSION['cooldown'] . ".<br>";
  36.             echo "You have " .
  37.             $time_remaining->h . " Hours " .
  38.             $time_remaining->i . " Minutes and " .
  39.             $time_remaining->s . " seconds remaining.";
  40.         }
  41.         /* User has waited for duration of timeout, lets remove the timeout */
  42.         elseif ($now > $cooldown_time) {
  43.             echo  "Okay you have waited long enough, lets remove that restriction...";
  44.             unset($_SESSION['cooldown']);
  45.             include 'form.php';
  46.         }
  47.     }
  48. ?>
  49. </body>
  50. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement