Reputation: 655
$_SESSION['guess1']=$_REQUEST['name'];
I'm trying to save that variable for my first guess for my mini hangman game, but whenever I put another input in, it changes. How could I save the request variable as a constant?
Upvotes: 0
Views: 118
Reputation: 3692
session_start();
if( !isset( $_SESSION['guesses'] )) {
$_SESSION['guesses'] = array(
$_REQUEST['name'];
);
} else {
$_SESSION['guesses'][] = $_REQUEST['name'];
}
This will create the $_SESSION['guesses'] array such that $_SESSION['guesses'][0] will contain the first guess, $_SESSION['guesses'][1] will contain the second, and so on. Also, count( $_SESSION['guesses'] );
will give you the total number of guesses made.
Upvotes: 1
Reputation:
You could try this:
session_start();
if (!isset($_SESSION['firstGuess'])) { // if firstguess not already set
$_SESSION['firstGuess'] = $_REQUEST['name']; // set it.
} else {
// second and subsequent guess code here.
}
Upvotes: 1