<?php
   // when a PHP is to use cookie-based sessions,
   //     it should call this before any output is sent to the
   //     browser

   session_start();
?>

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<!--
   try-session1.php

   Adapted from an old example that was originally at
   http://www.php.net/manual/en/re.session.php
   adapted by: Sharon Tuttle
   last modifiedZ: 2024-04-08

   can run this from the URL:
   https://nrs-projects.humboldt.edu/~st10/s24cs328/328lect12-1/try-session1.php
-->

<head>
    <title> Sessions, Take 1 </title>
    <meta charset="utf-8" />

<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
    if (! array_key_exists("count", $_SESSION))
    {
        ?>
	<h2> NOTE: count did NOT exist as a key in $_SESSION! </h2>

        <?php

        // add a key "count" to $_SESSION

        $_SESSION["count"] = 0;

        ?>
	<h3> ...but now have added it, with an initial value of
	     <?= $_SESSION["count"] ?> </h3>
	<?php
    }

    else
    {
        ?>
	<h2> NOTE: count DID exist in $_SESSION,
	     currently with the value: <?= $_SESSION["count"] ?> </h2>
        <?php
	$_SESSION["count"]++;

        ?>
	<h3> ...but now incremented that value, so it is now:
	     <?= $_SESSION["count"] ?> </h3>
	<?php
    }
    ?>
    
    <p> AFTER all that, count session attribute is now:
        <?= $_SESSION["count"] ?>
    </p>

    <?php
    // I decide that when/if count exceeds 7,
    //     I will destroy the current session

    if ($_SESSION["count"] > 7)
    {
        ?>
	<p> OH -- the count attribute's value is now greater
	    than 7, so DESTROYING the current session... </p>
	    
	<?php
	session_destroy();
    }
?>
<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>