You can't execute PHP code when the user exits a page. But what you want to do, can be achieved by a combination of session variables and checking if the form has been submitted.
Something like:
PHP Code:
<?php
session_start();
if (!isset($_POST['submit'])) {
// if the form hasn't been submitted, initialize the session variables
$_SESSION['randomnumber'] = // generate random number here;
$_SESSION['try'] = 1;
} else {
// if the form has been submitted, check the number inserted by the user against the random number in the session
if ($_SESSION['randomnumber'] == $_POST['name']) {
// do whatever you want to do when the user has guessed the number
} else {
$_SESSION['try']++;
if ($_SESSION['try'] > 5) {
// do whatever you want to do when the number of tries has been used
}
}
}
?>
<form action = "" method = "post">
Number : <input type = "text" name = "name" />
<input type = "submit" />
</form>
Bookmarks