views:

209

answers:

3

The first line of code, called on every one of my application's pages, is:

<?php require_once('../../_includes/initialize.php'); ?>

That file calls my classes, including SESSION. The SESSION class calls session_start() before anything is output to the browser.

On one page I'm using a form that has a ton of checkboxes, all with the name photo_select. I split up the photos into groups of 15, and then there are submit inputs across the top for pagination.

My goal is to catch the $_POST vars whenever a user moves to a new page and keep track of them. The only one that is complex is the checkbox array called photo_select, which keeps an array of any selected checkboxes and their values (which I've set to the photo ID for each).

Each time one of the pagination submit buttons is clicked, I need to take the submitted checkbox array and use array_merge to combine it with the saved array of previously selected checkboxes, so that I keep track of all selected photos from all pages.

I'd rather not post to a temp database to save on querying the database. It'd be a slight performance gain to keep the running array of selections in a $_SESSION variable.

To test this, I have the following code:

if( isset( $_POST['page'] ) ) {
    // Picking up the form data and assigning it to variables

    ... variables

    $photos = $_POST['photo-select'];

    $_SESSION['photo_holder'] = $photos;
}
echo isset($_SESSION['photo_holder']) ? 'true' : 'false';

SOLUTION:

change line: 

$_SESSION['photo_holder'] = $photos;

to:

if(isset($_POST['photo-select']) && !empty($_POST['photo-select'])) { $_SESSION['photo_holder'] = $photos; }

If I select 3 photos and click to another page, for example, it comes back as true (and the $_SESSION variable stores an array of 3 photo IDs). Which is perfect.

But then, if I don't click anymore photos on the new page, and move to another page, it comes back as false -- so the $_SESSION variable has been erased.


SOOOOOOOOO ***

Why oh why would the $_SESSION variable not persist?

+3  A: 
$photos = $_POST['photo-select'];    
$_SESSION['photo_holder'] = $photos;

if $_POST['photo-select']; is undefined $_SESSION['photo_holder'] gets bad

EDIT: If someone wants to try it himself

session_start();
$_SESSION['test'] = 2;
if (isset($_SESSION['test']))
 echo 2;
$_SESSION['test'] = $_POST['a'];  //missing POST variable
if (isset($_SESSION['test']))
 echo 2;
Svetlozar Angelov
By George I think you might be onto something!!!
Jason Rhodes
That was exactly it. Once I changed that line to if(isset($_POST['photo-select']) } it worked as expected. Genius!
Jason Rhodes
A: 

Well, currently it doesn't look like you're actually merging the array. I would assume that you're doing this in removed code, but I see you set $photos and then set $_SESSION[photo_holder']. So, every time I hit submit, $_SESSION['photo_holder'] isn't appending values (or removing those that have been unchecked). Instead, it's being overwritten by whatever is in $photos.

Jeff Rupert
That's correct. I need to be able to access the $_SESSION['photo_holder'] variable on the SECOND click, in order to merge it with the next round of checked boxes. Since it's not set on the 2nd click, I won't be able to do the array_merge().
Jason Rhodes
A: 

I've messed with $_SESSION only a small amount with little success. I'd recommend either

1) Using cookies to keep track of sessions on the client side

OR

2) Use HTML form posts to keep sessions active until the user initiated a "logout" routine or closes the browser

I'm partial to the second method. I use it a lot and it works great!!!!

Carlson Technology