title: Resetting/Stopping session PHP
body: <p>I've started to make a BlackJack game using as much PHP as possible and now i've got this:</p>
  
  <pre><code>&lt;?php
  session_start();
  
  // cards on pile
  if(!isset($_SESSION["pile"])) $_SESSION["pile"] = array(
    1           =&gt;  1,
    2           =&gt;  2,
    3           =&gt;  3,
    4           =&gt;  4,
    5           =&gt;  5,
    6           =&gt;  6,
    7           =&gt;  7,
    8           =&gt;  8,
    9           =&gt;  9,
    10          =&gt;  10,
    'Jack'      =&gt;  10,
    'Queen'     =&gt;  10,
    'King'      =&gt;  10,
    'Ace'       =&gt;  11);
  
  // cards in hand
  if(!isset($_SESSION["hand"])) $_SESSION["hand"] = array();
  
  // draw a card from the pile into the hand
  function draw_card() {
    $card = array_rand($_SESSION["pile"]);
    $_SESSION["hand"][$card] = $_SESSION["pile"][$card];
    unset($_SESSION["pile"][$card]);
  }
  
  function list_hand() {
    foreach($_SESSION["hand"] as $card=&gt;$points) {
      echo 'Card: ' . $card . '&lt;br&gt;';
      echo 'Points: ' . $points . '&lt;br&gt;&lt;br&gt;';
    }
  }
  
  // detect which form was triggered
  function FORM($value) {
    return isset($_POST["form"]) &amp;&amp; $_POST["form"]==$value;
  }
  
  // handle the draw form
  if(FORM("draw")){
  draw_card();
  list_hand();
  }
  ?&gt;
  
  &lt;form method="post"&gt;
    &lt;input type="hidden" name="form" value="draw"&gt;
    &lt;input type="submit" value="Draw a card"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Sorry for dropping a bomb of code on you guys.</p>
  
  <p>The problem with this is that when this page opens it will start the session. And give me standard 1 card, each time i refresh. (it will add up) I know this is the point of a session in the first place, but not the point of my page. I need the session to end when the page refreshes or if the page closes. I've had no luck searching the internet. Tried things as onUnload and many other things.</p>
  
  <p>Secondly Since it keeps adding cards untill all 14 are drawn. It will add one last <code>Card: 
  Points:</code> and at the top say <code>Notice: Undefined index</code>.</p>
  
  <p>I had my first contact with sessions using this very script. So i'm a bit of a loss where the end/reset my session and if i can bind this to a button.</p>
  
  <p>So to sum my problems up:</p>
  
  <p>Session needs to reset/stop upon refreshing or closing page.</p>
  
  <p>It shouldn't draw 1 standard card upon opening the page. (I'd rather have 0 and a button that will add 2, maybe a Start button or something)</p>
  
  <p>The session needs to automatically stop whenever the last card is drawn out of my array. (This isn't that important since in no case ever is it possible that one player in blackjack can hold all cards)</p>
  
  <p>Thanks in advance!</p>