The problem i currently have is that i open the script with session_start() and as we all know a session will remain untill you completely close your browser or within a certain timelimit. This is exactly my problem.
I need to make it so that if a Start Button is pressed it will session_start() but when you click the Reset Button the session needs to 'reset'.
I've read countless pages about sessions and i've read thousends of questions already on stackoverflow. But i really can't find the solution for my problem.
I've heard someone say they bound it to if button pressed, session_start = true. But i couldn't find any explanation for it.
What i'm trying to make is a little game. A BlackJack game. And since i need to use as much PHP as possible i'm currently using this:
<form method="post">
<input type="hidden" name="form" value="draw"/>
<input type="submit" value="Hit"/>
</form>
Then im using this PHP with it:
<?php
session_start();
include_once 'cards_array.php';
// cards in hand
if(!isset($_SESSION["hand"])) $_SESSION["hand"] = array();
// draw a card from the pile into the hand
function draw_card() {
if(count($_SESSION["pile"]) > 0){
$card = array_rand($_SESSION["pile"]);
$_SESSION["hand"][$card] = $_SESSION["pile"][$card];
unset($_SESSION["pile"][$card]);
}else{
unset($_SESSION["pile"]);
unset($_SESSION["hand"]);
session_destroy();
}
}
function list_hand() {
foreach($_SESSION["hand"] as $card=>$points) {
echo $card;
echo ', ';
}
}
// detect which form was triggered
function FORM($value) {
return isset($_POST["form"]) && $_POST["form"]==$value;
}
// handle the draw form
if(FORM("draw")){
draw_card();
list_hand();
}
include_once 'cards_cases.php';
?>
With cards_array being:
<?php
// cards on pile
if(!isset($_SESSION["pile"])) $_SESSION["pile"] = array(
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
7 => 7,
8 => 8,
9 => 9,
10 => 10,
'Jack' => 10,
'Queen' => 10,
'King' => 10,
'Ace' => 11);
?>
Sorry for dropping this code bomb on you guys, but since i don't really know what's important or what can be left out and risk you asking where is x and where is y, i'd rather place most of it in the hope its clear for you what im trying to accomplish.
Since i've never worked with sessions before this, i'm really at a loss as what i can do for my problem. I don't know if using my code this way is the best way. But as long as it's PHP and can be improved please let me know!
So basically my problem is that i need to start and end a session using buttons.
I hope i explained as good as possible what im struggling with and I hope one of you has the answer i'm looking for!
Thanks in advance!