tomaytotomato
tomaytotomato

Reputation: 4028

Very strange $_SESSION behaviour

I have a Session which I am using to hold items in a form that are accumulated up by the user until the user wants to proceed to checkout. Its a bit like a Shopping cart where items can be added from the form.

Logical breakdown of code:

  1. Page loads, session starts
  2. If $_SESSION['set'] is not set then set it to TRUE.
  3. Display rest of page and form.
  4. User hits "Add another item" button.
  5. Page data gets posted to itself
  6. Page checks that $_SESSION['set'] = True and $_POST['add_item'] is set.
  7. Page creates a session variables in an array, and adds posted values to those sessions.
  8. Page increments $_SESSION['tariff_count'] if more needs to be added

The problem is that my code is not behaving as it should. When I click "Add new tariff" button the first time it does not get caught by my if function. This should be immediately caught. However when I go and press the button again, it finally works and adds an item to my session.

Here is the code:

//start a session to remember tariff items 
session_start();

//testing the session array
print_r($_SESSION);

//destroy session if this character is found in URL string
$des = $_GET['d'];
if($des == 1)
{
  session_destroy();
}

//checks to see if session data has been set

//if a session variable count is set then 
if ($_SESSION['set'] == TRUE)
{
  //perform a check to ensure the page has been called by the form button and not been accidently refreshed
  if(isset($_POST['add_tariff']))
  { 
    //if user clicks Add another tariff button then increase tariff count by one

    //temp variable set to the current count of items added
    $count = $_SESSION['tariff_count'];
    $_SESSION['tariff_name'][$count] = $_POST['tariff_name'];
    $_SESSION['tariff_net'][$count] = $_POST['tariff_net'];
    $_SESSION['tariff_inclusive'][$count] = $_POST['tariff_inclusive'];
    $_SESSION['tariff_length'][$count] = $_POST['tariff_length'];
    $_SESSION['tariff_data'][$count] = $_POST['tariff_data'];
    //increment tariff count if more data needs to be added to the sessions later.
    $_SESSION['tariff_count']++;
  }
}
//if no session data set then start new session data
else
{   
  echo "session set";
  $_SESSION['set'] = TRUE;
  $_SESSION['tariff_count'] = 0;
}

The code seems to be fudging my arrays of Sesssion data. All my added items in the session are displayed in a table.

However if my table shows six items, if i do a print_r of the session it only shows there are 4 items in the array? I have tested it to make sure I am not reprinting the same instances in the array.

Here is a print_r of the array that shows six rows but there are only four rows in this array?

[tariff_count] => 5 [tariff_name] => Array (
    [0] => STREAM1TARIFF [1] => STREAM1TARIFF [2] => CSS [3] => CSS [4] => CSS
  ) 

I have take a screenshot as well to show this strange problem

https://i.sstatic.net/ARv1F.png

Note I have echoed out "True Value =6" but in the print_r of the session it is only 5, so my code is missing out one instance (n-1).

Here is my code that prints all the instances in the session arrays, I have a feeling part of the problem in mismatch is caused by the "<=" comparison?

if(isset($_SESSION['tariff_count']))
{
  for ($i = 0; $i <= $count; $i++)
  {
    echo "<tr>";
    echo "<td>".$_SESSION['tariff_name'][$i]."</td>";
    echo "<td>".$_SESSION['tariff_net'][$i]."</td>";
    echo "<td>".$_SESSION['tariff_inclusive'][$i]."</td>";
    echo "<td>".$_SESSION['tariff_length'][$i]."</td>";
    echo "<td>".$_SESSION['tariff_data'][$i]."</td>";
    echo "</tr>";
  }
}

Paste bin of php page - http://pastebin.com/petkrEck

Any ideas, why my If statement is not catching the event when the user presses "Add another tariff" button the first time it is pressed, but then detects it afterwards?

Thanks for your time

Merry Christmas!

Upvotes: 2

Views: 210

Answers (1)

Marc B
Marc B

Reputation: 360662

The problem is your code flow. In simplified pseudo-code, you're doing this:

if (session is not initialized) {
    set = true
    count = 0;
} else {
    add posted data to session
}

On the first 'add item' call, the session is not set up, so you set up the session. AND THEN IGNORE THE POSTED DATA.

The code flow should be:

if (session is not initialized) {
    set = true;
    count = 0;
}

if (posting data) {
   add data to session
}

Upvotes: 3

Related Questions