Austin's Cookie Demo

This demonstrates how a COOKIE works.

The current value of the cookie is:


Cookie Variables

Code follows

<?php

// code;

error_reporting(E_ALL);    // set error reporting to all
ini_set('display_errors', 1);
$self = basename($_SERVER['SCRIPT_NAME']);

// code

$cook = '';
$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))
        {
            
$full_variable = $first_variable . ' ' . $second_variable;  // +60*60*24*365 = 1 year
            
setcookie('variable', $full_variable, time() + 60);    // NOTE: +60 = 60 seconds    (+3600 = one hour)
        
}
        break;

    case 
'Refresh':
        break;

    case 
'Delete Cookie':

        
/* Note the time is set to past so the cookie will time out */
        
setcookie('variable', '', time() - 86400);    // -86400 = -one day (-24 hours/yesterday)
        
break;
}

if (isset(
$_COOKIE['variable']))
{
    
$cook = $_COOKIE['variable'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Cookie</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 Cookie Demo</h1>

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

    <ul class="circle">
        <li>Submit will write a COOKIE to your computer using the first and second variables you provide below.</li>
        <li>Refresh will read the COOKIE from your computer and display contents below.</li>
        <li>Delete will delete the COOKIE from your computer.</li>
        <li>Additional note -- the COOKIE will automatically expire in 60 seconds. To see this happen, wait 60 seconds and
            Refresh
        </li>
    </ul>

    <p>
        The current value of the cookie is: <span class="red"><?php echo(htmlspecialchars($cook)); ?> </span>
    </p>

    <hr>

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

            <fieldset>
                <legend>Cookie 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 Cookie">
    </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>