<?php // when a PHP is to use cookie-based sessions, // NEEEEEEED to call the statement below // before any output is sent to the browser session_start(); ?> <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <!-- Our first session example! Adapted from an old example that was originally at http://www.php.net/manual/en/re.session.php (but no longer exists when I checked in Spring 2026) by: Sharon Tuttle last modified: 2026-04-21 you can run this using the URL: https://nrs-projects.humboldt.edu/~st10/s26cs328/328lect13-1/try-session1.php --> <head> <title> PHP Sessions, Take 1 </title> <meta charset="utf-8" /> <?php // turn on error reporting ini_set('display_errors', 1); error_reporting(E_ALL); ?> <link href="https://nrs-projects.humboldt.edu/~st10/styles/normalize.css" type="text/css" rel="stylesheet" /> </head> <body> <h1> playing with PHP sessions </h1> <?php // when this PHP is requested, if "count" is NOT a $_SESSION array key, // make it such a key and give $_SESSION["count"] the value 0 if (! array_key_exists("count", $_SESSION)) { ?> <h2> NOTE: count did NOT exist as a key in $_SESSION! </h2> <?php // MAKE a key count in $_SESSION // let's add a session key "count", and give it the value 0 $_SESSION["count"] = 0; ?> <h3> ...but now HAVE added it, with an initial value of: <?= $_SESSION["count"] ?> </h3> <?php } // if we get here, "count" WAS found to be a key in $_SESSION else { ?> <h2> NOTE: count DID exist as a key in $_SESSION, currently with value: <?= $_SESSION["count"] ?> </h2> <?php // I decide to increment its value $_SESSION["count"]++; ?> <h3> ...but now incremented that value, so it is now: <?= $_SESSION["count"] ?> </h3> <?php } ?> <p> AFTER that initial part, $_SESSION["count"] is now: <?= $_SESSION["count"] ?> </p> <?php // I decide that when/if the count exceeds 7, I will destroy the // current session if ($_SESSION["count"] > 7) { ?> <p> OH the count session key's value is now greater than 7, so DESTROYING the current session... </p> <?php session_destroy(); } ?> <p> <a href="<?= htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES) ?>"> Try again </a> </p> <footer> <hr /> <p> Validate by pasting .xhtml copy's URL into<br /> <a href="https://validator.w3.org/nu"> https://validator.w3.org/nu </a> or <a href="https://html5.validator.nu/"> https://html5.validator.nu/ </a> </p> </footer> </body> </html>