Veronica's SESSION Demo: A Guessing Game

This page chooses a semi-random number, a semi-random color, and a semi-random food item and then stores them in a SESSION. It also tracks how many times you visit page 1. You will be able to see these in the "Hint" below and on the next page. Click ~See Results~ to get to the next page.

Can you guess what 3 things I will choose?
Guess a number between 2 - 99, a color, and a food item.
Then click the ~See Results~ button to see them on another page.

This session id = e70126b0a611ee8676f78f3d2ef5072f


  Hint:  32 yellow eggplants 
Visit count = 1
 

CODE FOLLOWS

<?php
// name the session and start it
session_name("guessing_game");
session_start();

include(
'includes/header.php');

// set error reporting to all
error_reporting(E_ALL);

// set variables
$num = isset($_SESSION['num']) ? $_SESSION['num'] : null;
$color = isset($_SESSION['color']) ? $_SESSION['color'] : null;
$food = isset($_SESSION['food']) ? $_SESSION['food'] : null;
$count = isset($_SESSION['count']) ? $_SESSION['count'] : 0;
$i 0;

// get values from functions into variables
$num get_num();
$color get_color();
$food get_food();

// count how many times user accesses this session
$count++;

// add values to SESSION array
$_SESSION['num'] = $num;
$_SESSION['color'] = $color;
$_SESSION['food'] = $food;
$_SESSION['count'] = $count;
?>

<h1>Veronica's SESSION Demo: A Guessing Game</h1>

<p>
    This page chooses a semi-random number, a semi-random color, and a semi-random
    food item and then stores them in a SESSION. It also tracks how many times
    you visit page 1. You will be able to see these in the "Hint" below and
    on the next page. Click ~See Results~ to get to the next page.
</p>
<p>
    Can you guess what 3 things I will choose?
    <br>Guess a number between 2 - 99, a color, and a food item.
    <br> Then click the ~See Results~ button to see them
    on another page.
</p>

<?php
echo('<p>This session id = ' session_id() . '</p>');
?>
<form action="session_array_page2.php" method="post">
    <input type="submit" value=" See Results ">
</form>
<br>
<?php
echo('<pre><small>  Hint:  ');
foreach (
$_SESSION as $s)
{
    
// display all except count from session array
    
if ($i 3)
    {
        echo(
$s ' ');
    }
    
$i++;
}
echo(
"<br>  Visit count = $count </small></pre>");

// function to get a semi-random number
function get_num()
{
    
// get random number between 2 and 99
    
$r rand(2,99);
    return 
$r;
}

// function to get a semi-random color
function get_color()
{
    
// put colors in an array
    
$colors = array("red","purple","blue","green","yellow","orange","peach","lavender",
        
"aquamarine","brown","white","black","gray");

    
// get random index number
    
$r rand(0,12);
    
$c $colors[$r];
    return 
$c;
}

// function to get a semi-random food item
function get_food()
{
    
// put foods in an array
    
$foods = array("berries","eggplants","oysters","pies","mushrooms","olives","eggs","beans",
        
"steaks","blocks of cheese","noodles","hot dogs","bagels");

    
// get random index number
    
$r rand(0,12);
    
$f $foods[$r];
    return 
$f;
}

?>

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