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?