[SOLVED] saving an array into a session var
Posted: Mon Jul 04, 2005 4:07 pm
Hi guys
I'm gradually getting to grips with arrays.
And I'm just starting to use sessions.
I've no problems with either concepts, but I'm struggling trying to save an array into a session variable.
This is a test I've made, and I want the time of the page generation to be appended to the array $currentplaylist, and then saved into a session variable.
But it seems my code just clears my array, and adds the new value.
I know my session is working as I get the next previous displayed with the print_r function at the bottom.
Here's my code
Any suggestions as to how I get this to work?
I was under the impression that a session variable could be an array, and if you pass an array to it, then it stores it as an array.
So I think there's something wrong with the way I'm starting my array initially.
Thanks
Benis is a test I've made, and I want the time of the page generation to be appended to the array $currentplaylist, and then saved into a session variable.
But it seems my code just clears my array, and adds the new value.
I know my session is working as I get the next previous displayed with the print_r function at the bottom.
Here's my code
Any suggestions as to how I get this to work?
I was under the impression that a session variable could be an array, and if you pass an array to it, then it stores it as an array.
So I think there's something wrong with the way I'm starting my array initially.
Thanks
Ben
I'm gradually getting to grips with arrays.
And I'm just starting to use sessions.
I've no problems with either concepts, but I'm struggling trying to save an array into a session variable.
This is a test I've made, and I want the time of the page generation to be appended to the array $currentplaylist, and then saved into a session variable.
But it seems my code just clears my array, and adds the new value.
I know my session is working as I get the next previous displayed with the print_r function at the bottom.
Here's my code
Code: Select all
session_start();
header("Cache-control: private"); // THIS IS THAT IE6 BUG FIX I READ ABOUT
$_SESSION = array();
$playlistadd = $_GET["playlistadd"];
if ( isset($_GET["playlistadd"])) {
$currentplaylist = $_SESSION["currentplaylist"];
if ( !is_array($currentplaylist)) {
// IT'S NOT AN ARRAY, SO MAKE IT INTO ONE
$currentplaylist = array();
}
// ADD $playlistadd TO OUR $currentplaylist ARRAY
array_push($currentplaylist, $playlistadd);
// SAVE THE SESSION
$_SESSION["currentplaylist"] = $currentplaylist;
}
$time = time();
echo <<<HTML
<a href="sessionstest.php?playlistadd=$time">click here to add an item to our array</a>
<br />
<hr />
HTML;
echo "<pre>";
print_r($currentplaylist);
echo "</pre>";I was under the impression that a session variable could be an array, and if you pass an array to it, then it stores it as an array.
So I think there's something wrong with the way I'm starting my array initially.
Thanks
Benis is a test I've made, and I want the time of the page generation to be appended to the array $currentplaylist, and then saved into a session variable.
But it seems my code just clears my array, and adds the new value.
I know my session is working as I get the next previous displayed with the print_r function at the bottom.
Here's my code
Code: Select all
session_start();
header("Cache-control: private"); // THIS IS THAT IE6 BUG FIX I READ ABOUT
$_SESSION = array();
$playlistadd = $_GET["playlistadd"];
if ( isset($_GET["playlistadd"])) {
$currentplaylist = $_SESSION["currentplaylist"];
if ( !is_array($currentplaylist)) {
// IT'S NOT AN ARRAY, SO MAKE IT INTO ONE
$currentplaylist = array();
}
// ADD $playlistadd TO OUR $currentplaylist ARRAY
array_push($currentplaylist, $playlistadd);
// SAVE THE SESSION
$_SESSION["currentplaylist"] = $currentplaylist;
}
$time = time();
echo <<<HTML
<a href="sessionstest.php?playlistadd=$time">click here to add an item to our array</a>
<br />
<hr />
HTML;
echo "<pre>";
print_r($currentplaylist);
echo "</pre>";I was under the impression that a session variable could be an array, and if you pass an array to it, then it stores it as an array.
So I think there's something wrong with the way I'm starting my array initially.
Thanks
Ben