Create and retrieve a cookie


SUBMITTED BY: henry1874w

DATE: June 21, 2017, 11:29 p.m.

FORMAT: Text only

SIZE: 532 Bytes

HITS: 232

  1. <!DOCTYPE html>
  2. <?php
  3. $cookie_name = "user";
  4. $cookie_value = "John Doe";
  5. setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
  6. ?>
  7. <html>
  8. <body>
  9. <?php
  10. if(!isset($_COOKIE[$cookie_name])) {
  11. echo "Cookie named '" . $cookie_name . "' is not set!";
  12. } else {
  13. echo "Cookie '" . $cookie_name . "' is set!<br>";
  14. echo "Value is: " . $_COOKIE[$cookie_name];
  15. }
  16. ?>
  17. <p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p>
  18. </body>
  19. </html>

comments powered by Disqus