fork download
  1. <?php
  2.  
  3. // Function to display session data
  4. function displaySessionData() {
  5. if(isset($_SESSION['name']) && isset($_SESSION['age']) && isset($_SESSION['city'])) {
  6. echo "Name: " . $_SESSION['name'] . "<br>";
  7. echo "Age: " . $_SESSION['age'] . "<br>";
  8. echo "City: " . $_SESSION['city'] . "<br>";
  9. } else {
  10. echo "Session data not set.";
  11. }
  12. }
  13.  
  14. // Check if form is submitted
  15. if(isset($_POST['submit'])) {
  16. // Store form data in session variables
  17. $_SESSION['name'] = $_POST['name'];
  18. $_SESSION['age'] = $_POST['age'];
  19. $_SESSION['city'] = $_POST['city'];
  20. }
  21.  
  22. // Check if user wants to delete session
  23. if(isset($_POST['delete'])) {
  24. // Unset session variables
  25. unset($_SESSION['name']);
  26. unset($_SESSION['age']);
  27. unset($_SESSION['city']);
  28. // Destroy session
  29. }
  30.  
  31. ?>
  32.  
  33. <!DOCTYPE html>
  34. <html>
  35. <head>
  36. <title>Session Example</title>
  37. </head>
  38. <body>
  39. <h2>Session Example</h2>
  40. <h3>Enter Your Information:</h3>
  41. <form method="post" action="">
  42. Name: <input type="text" name="name"><br>
  43. Age: <input type="text" name="age"><br>
  44. City: <input type="text" name="city"><br>
  45. <input type="submit" name="submit" value="Set Session">
  46. </form>
  47.  
  48. <h3>Session Data:</h3>
  49. <?php displaySessionData(); ?>
  50.  
  51. <h3>Modify Session:</h3>
  52. <form method="post" action="">
  53. Name: <input type="text" name="name"><br>
  54. Age: <input type="text" name="age"><br>
  55. City: <input type="text" name="city"><br>
  56. <input type="submit" name="submit" value="Modify Session">
  57. </form>
  58.  
  59. <h3>Delete Session:</h3>
  60. <form method="post" action="">
  61. <input type="submit" name="delete" value="Delete Session">
  62. </form>
  63. </body>
  64. </html>
  65.  
Success #stdin #stdout 0.03s 25996KB
stdin
kala
23
cbe
stdout
<!DOCTYPE html>
<html>
<head>
    <title>Session Example</title>
</head>
<body>
    <h2>Session Example</h2>
    <h3>Enter Your Information:</h3>
    <form method="post" action="">
        Name: <input type="text" name="name"><br>
        Age: <input type="text" name="age"><br>
        City: <input type="text" name="city"><br>
        <input type="submit" name="submit" value="Set Session">
    </form>

    <h3>Session Data:</h3>
    Session data not set.
    <h3>Modify Session:</h3>
    <form method="post" action="">
        Name: <input type="text" name="name"><br>
        Age: <input type="text" name="age"><br>
        City: <input type="text" name="city"><br>
        <input type="submit" name="submit" value="Modify Session">
    </form>

    <h3>Delete Session:</h3>
    <form method="post" action="">
        <input type="submit" name="delete" value="Delete Session">
    </form>
</body>
</html>