Advertisement
vvccs

wt_12_session_cookie

Oct 13th, 2024
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. access cookie
  2. <?php
  3. // Check if the cookie is set
  4.  
  5. if (isset($_COOKIE["username"])) {
  6. echo "Welcome back, " . $_COOKIE["username"] . "!";
  7. } else {
  8. echo "No cookie found. Please log in.";
  9. }
  10. ?>
  11.  
  12. cookie example
  13. <?php
  14. // Set a cookie that lasts for 1 hour
  15. setcookie("username", "JohnDoe", time() + 3600, "/"); // "/" makes it available across the whole domain
  16. echo "Cookie has been set.";
  17. ?>
  18.  
  19. <?php
  20. // Delete the cookie by setting its expiration date to the past
  21. setcookie("username", "", time() - 3600, "/");
  22. echo "Cookie has been deleted.";
  23. ?>
  24.  
  25.  
  26. session example
  27. <?php
  28. // Start the session session_start();
  29. // Store session variables
  30. $_SESSION["username"] = "JohnDoe";
  31. $_SESSION["email"] = "johndoe@example.com"; echo "Session variables are set.";
  32. ?>
  33.  
  34.  
  35. active session
  36. <?php
  37. // Start the session session_start();
  38. // Store session variables
  39. $_SESSION["username"] = "JohnDoe";
  40. $_SESSION["email"] = "johndoe@example.com"; echo "Session variables are set.";
  41. ?>
  42.  
  43. destroy session
  44. <?php
  45. // Start the session session_start();
  46. // Remove all session variables session_unset();
  47. // Destroy the session
  48. session_destroy();
  49. echo "Session destroyed. You have been logged out.";
  50. ?>
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement