alexswear
alexswear

Reputation: 655

Session variables changing on refresh/button click

<?php session_start(); ?>
$_SESSION['catRandNum'] = rand(0,2);

That is one of my variables randomly selecting a category for my hangman mini-game I am making. Before Refresh

After Refresh

Is there any way I could make the variable stay constant so the category will stay the same? Thank you for your help.

Upvotes: 0

Views: 1013

Answers (1)

Barmar
Barmar

Reputation: 781370

maybe this is what you want:

if (!isset($_SESSION['catRandNum'])) {
    $_SESSION['catRandNum'] = rand(0,2);
}

If this is a new session, the variable is set randomly. Otherwise it keeps its value.

Upvotes: 4

Related Questions