Stephs's Cookie Demo

This demonstrates how a COOKIE works.

The current value of the cookie is:


Cookie Variables


<?php

// code;

error_reporting(E_ALL);    // set error reporting to all
$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'];
}

include(
'../header/assignmentHeader.php');
?>
<link rel="stylesheet" href="../css/assignmentHeaderStyles.css" />

<h1>Stephs'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($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>

<br>

<?php
show_source
(__FILE__);
include(
'../footer/footer.php');
?>