Blog

How to Save a Random Number as a Session Variable in PHP
Posted on June 18, 2015 in PHP by Matt Jennings

<?php
session_start();

/*
If statement below saves a random number as session variable
so it won't change when a form is submitted multiple times
until the session is destroyed
*/
if(!isset($_SESSION['rand_num_session']))
{
    $_SESSION['rand_num_session'] = rand(1, 100);
}

$_SESSION['my_guess'] = $_POST['guess'];

header('Location: great-number-game.php');
?>

Leave a Reply