Morning! I'm creating a game where each user, after having answered several questions of a form, will be redirected to another page where they will play a game to find out if they win a prize or not (it won't depend on the answer, it will be casual).
The goal is to limit their attempt to once per day and to achieve I would use both the database and the session (I guess). At this very phase of the development, I'm trying to achieve it only with a session, then I hope to figure it out if I need to match that value in the database and so on.
So far so good, that's my attempt:
game.php
<form action="result.php" method="POST">
THE INDIPENDENT GAME
<input type="submit" value="button" name="mybutton">
</form>
<?php
session_start();
$_SESSION['id'] = session_id();
$_SESSION['time'] = time();
if ($_SESSION["count"] !== 0) {
$_SESSION["count"] = 0;
}
function getrandomimage() {
$imageplay = ['400.png', '500.png', 'sorry.png'];
$randKeys = array_rand($imageplay, 1);
return $_SESSION['getrandomimage'] = $imageplay[$randKeys];
}
getrandomimage();
var_dump($_SESSION);
?>
result.php
<?php
session_start();
if (!isset($_POST['mybutton'])) {
$_SESSION["count"]++;
}
var_dump($_SESSION);
?>
As I said, the game won't depend on the form. To better understand my code, the final user will have an image to scratch and he/she will find out if he/she will win something: there will be an image layout and underneath three options which are 400.png, 500.png and sorry.png.
I created a function called getrandomimage() that will do the trick, and I tried to store the variable generated. There will be a couple of problems: if I will come back to game.php, the session will automatically start from 0 and if I refresh the page result.php, the count of $_SESSION["count"] will increment by one.
The goal is, as soon as a user has started a session, $_SESSION['getrandomimage'] will stay the same for that session, which will expire after a day so the user won't try the game more than once, even if he/she will get back to the previous page.
Aucun commentaire:
Enregistrer un commentaire