PHP $_SESSION
With SESSION, data is stored on server side, COOKIE store data on users browser. COOKIE can be edited by user, so it is better use SESSION whenever possible.
In PHP, SESSION can be accessed with Array $_SESSION
Like COOKIE, SESSION must be set before you output any text to browser (echo, blank lines etc..).
Before you can use $_SESSION, you need to start session with PHP function
PHP Code:
session_start();
day_11_ex_1.php
PHP Code:
<?php
// Lets start session.
session_start();
// check if session variable is set.
if (isset($_SESSION['hitCounter'])) {
echo '<p>Welcome, you visited this page ' . $_SESSION['hitCounter'] . ' times before.</p>';
// This will increase value of hitCounter by 1 each time you refresh the page.
$_SESSION['hitCounter'] = $_SESSION['hitCounter'] + 1;
} else {
echo '<p>Welcome, this is your first visit to this page.</p>';
$_SESSION['hitCounter'] = 1;
}
Unlike cookie, SESSION only store data as long as your browser is open. If you close browser, data stored in session will be lost.
SESSIONS are mostly used to see if a user have logged in to a site or not.
Number Guessing Game With SESSION
Let's update our Number Guessing Game code with PHP SESSION support. Now this game is cheat proof, no one can cheat the game and win, try it your self.
day_11_ex_2.php
PHP Code:
<?php
// We need to call session_start() function before
// using session.
session_start();
// check if session is set, if yes, read secretNumber from session.
// If no session set, generate a random number, store it in session.
if (isset($_SESSION['secretNumber'])) {
$secretNumber = $_SESSION['secretNumber'];
} else {
$secretNumber = rand(1,100);
$_SESSION['secretNumber'] = $secretNumber;
}
// if user win the game, we need to generate another number
// and store it in SESSION, so he can play again. If not,
// a user can play only once, then restart browser to play again.
// Try commenting code below and see.
if (isset($_POST['userNumber'])) {
if ($_POST['userNumber'] == $secretNumber) {
$_SESSION['secretNumber'] = rand(1,100);
}
}
?>
<html>
<body>
<h1>Number Guessing Game.</h1>
<?php
if (isset($_POST['userNumber'])) {
$userNumber = $_POST['userNumber'];
if ($secretNumber > $userNumber) {
echo '<h1 style="color:red">Your number is too SMALL.</h1>';
} else if ($secretNumber < $userNumber) {
echo '<h1 style="color:blue">Your number is too BIG.</h1>';
} else {
echo '<h1 style="color:green">You win the game.</h1>';
}
}
?>
<form method="POST" action="">
Enter Number: <input name="userNumber" type="text">
<button type="submit">Check</button>
</form>
<h2>How to Play:</h2>
<pre>
* Server will pick a secret number between 1 and 100.
* You guess what number it is.
* If your guess is too high or too low, Server will give you a hint.
* See how many turns it takes you to win!
</pre>
</body>
</html>
What is the proper way to play this game ?
We know the secretNumber is between 1 and 100, so first try 50.
If it says userNumber is low, try 75.
If it says userNumber is high, try 25.
Continue the same, until you reach the secretNumber. This is called Minimax (MinMax) decision rule.
Number Guessing Game With Number of Try
Lets also display number of try required to get the answer. Skip this part if you don't understand.
day_11_ex_3.php
PHP Code:
<?php
// We need to call session_start() function before
// using session.
session_start();
// check if session is set, if yes, read secretNumber from session.
// If no session set, generate a random number, store it in session.
if (isset($_SESSION['secretNumber'])) {
$secretNumber = $_SESSION['secretNumber'];
$try = $_SESSION['try'];
$_SESSION['try'] = $_SESSION['try'] + 1;
} else {
$secretNumber = rand(1,100);
$try = 1;
$_SESSION['secretNumber'] = $secretNumber;
$_SESSION['try'] = $try;
}
// if user win the game, we need to generate another number
// and store it in SESSION, so he can play again. If not,
// a user can play only once, then restart browser to play again.
// Try commenting code below and see.
if (isset($_POST['userNumber'])) {
if ($_POST['userNumber'] == $secretNumber) {
$_SESSION['secretNumber'] = rand(1,100);
$_SESSION['try'] = 1;
}
}
?>
<html>
<body>
<h1>Number Guessing Game.</h1>
<?php
if (isset($_POST['userNumber'])) {
$userNumber = $_POST['userNumber'];
if ($secretNumber > $userNumber) {
echo '<h1 style="color:red">Your number ' . $userNumber . ' is too SMALL.</h1>';
} else if ($secretNumber < $userNumber) {
echo '<h1 style="color:blue">Your number ' . $userNumber . ' is too BIG.</h1>';
} else {
echo '<h1 style="color:green">You win the game.</h1>';
}
echo '<h2>Trail Number ' . $try . '.</h2>';
}
?>
<form method="POST" action="">
Enter Number: <input name="userNumber" type="text">
<button type="submit">Check</button>
</form>
<h2>How to Play:</h2>
<pre>
* Server will pick a secret number between 1 and 100.
* You guess what number it is.
* If your guess is too high or too low, Server will give you a hint.
* See how many turns it takes you to win!
</pre>
</body>
</html>
Bookmarks