0

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!

3 Answers 3

2

create a logout page like this

logout.php

<?php   
session_start(); // start the same session used before
session_destroy(); // destroy all session variables
header("location: /yourpage.php"); // redirect
exit();
?>

Your html

...
<a href="logout.php">LOGOUT</a>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks so much! this fixes the reset button exactly as i want to! If I use it this way, isn't it better for me to set the main page as the logout.php, this way if they close the browser and go to logout.php again it will be resetted again?
You can play with $_GET variables an create conditions to destroy sessions.
This isn't really question related but if you can answer it it would save me alot of time and other peoples. Is it posisble to run a php function upon a button click. Without using JavaScript? As far as i know you cant use onclicks using PHP but maybe there is a workaround for it? Thanks again!
You may thing about attaching an event into a button, but you need a different approach, use web forms with submit events (GET or POST) then you can get the button value and execute some code in the backend. If you want it live use ajax or jquery $.ajax
1

you can remove a single variable in the session

unset($_SESSION['session_var']);

or this will remove all the variables in the session, but not the session itself

session_unset(); 

this will destroy the session variables

session_destroy(); 

1 Comment

Thanks for your answer, but i wouldn't be making a question this long among all the similar questions if i didn't already try this. The session_destroy, works just fine in the cards_cases when my points are >21.
0

Once the page is loaded no more PHP scripts are executed, so you need to send an AJAX request to the server when you press the button and start session on it.

Probably you will need to read about jQuery an AJAX searching on google.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.