Austin's Session Demo

This demonstrates how a SESSION works.

The current session ID is: 7e8d77d719984641ee9363b3e940896f

$_SESSION['first_variable'] is:

$_SESSION['second_variable'] is:


Session Variables

Code follows

<?php

// code;

error_reporting(E_ALL);    // set error reporting to all
ini_set('display_errors', 1);

session_start();

$self = basename($_SERVER['SCRIPT_NAME']);

// code

$first_variable = '';
$second_variable = '';

$submit = isset($_POST['submit']) ? $_POST['submit'] : null;

switch (
$submit)
{
    case 
'Submit':

        
$first_variable = isset($_POST['first_variable']) ? $_POST['first_variable'] : null;
        
$second_variable = isset($_POST['second_variable']) ? $_POST['second_variable'] : null;

        if ((
$first_variable != null && $second_variable != null))
        {

            
$_SESSION['first_variable'] = $first_variable;
            
$_SESSION['second_variable'] = $second_variable;
        }
        break;

    case 
'Refresh':
        break;

    case 
'Delete Session':

        
$_SESSION = array();
        
session_destroy();
        break;
}

$first_variable = isset($_SESSION['first_variable']) ? $_SESSION['first_variable'] : '';
$second_variable = isset($_SESSION['second_variable']) ? $_SESSION['second_variable'] : '';
$session_id = session_id();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Session</title>
    <link rel="stylesheet" href="../css/styles.css">
</head>
<body>

<div class="page">

    <p class="nav"><a href="../../index.php">Home</a></p>

    <h1>Austin's Session Demo</h1>

    <h2>
        This demonstrates how a SESSION works.
    </h2>

    <ul class="circle">
        <li>Submit will write the first and second variables you provide below into the SESSION (stored on the server).</li>
        <li>Refresh will reload the page and read the values back out of the SESSION to display below.</li>
        <li>Delete will clear and destroy the SESSION.</li>
    </ul>

    <p>
        The current session ID is: <span class="red"><?php echo(htmlspecialchars($session_id)); ?> </span>
    </p>

    <p>
        $_SESSION['first_variable'] is: <span class="red"><?php echo(htmlspecialchars($first_variable)); ?> </span>
    </p>

    <p>
        $_SESSION['second_variable'] is: <span class="red"><?php echo(htmlspecialchars($second_variable)); ?> </span>
    </p>

    <hr>

    <form name="my_form" action="<?php echo($self); ?>" method="post">
        <table>

            <fieldset>
                <legend>Session Variables</legend>
                <label for="first_variable">First Variable:</label>
                <input type="text" size="36" id="first_variable" name="first_variable" value="">
                <br>
                <label for="second_variable">Second Variable:</label>
                <input type="text" size="36" id="second_variable" name="second_variable" value="">
            </fieldset>

        </table>
        <br>
        <input type="submit" name="submit" value="Submit">
        <input type="submit" name="submit" value="Refresh">
        <input type="submit" name="submit" value="Delete Session">
    </form>

    <h2>Code follows</h2>

    <div class="code"><?php highlight_file(__FILE__); ?></div>

    <p class="nav nav-foot"><a href="../../index.php">Home</a></p>

</div>

</body>
</html>