Page 1 of 1

[SOLVED] saving an array into a session var

Posted: Mon Jul 04, 2005 4:07 pm
by batfastad
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

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>";
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

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>";
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

Posted: Mon Jul 04, 2005 4:47 pm
by Burrito
well you're setting $_SESSION = array() is not correct. The $_SESSION is already an array.

you need to use a multidimensional array if you want to set a session value to an array:

ex:

Code: Select all

$bob = array('larry','joe');
$_SESSION['names'] = $bob;
print_r($_SESSION['names']);
$bob = array('larry','joe');
$_SESSION['names'] = $bob;
print_r($_SESSION['names']);


Posted: Mon Jul 04, 2005 5:09 pm
by batfastad
Removing
$_SESSION = array();

Did the trick!

I didn't think that through, as at the start of the php script I was clearing my session by doing that.

Duh!

I was following the tutorial
http://www.phpfreaks.com/print.php?cmd= ... &tut_id=41

And of course, it mentions the $_SESSION = array(); in the 'how to destroy a session' section.

Thanks for pointing this out as I thought that my actual initialisation of the array was correct, I just messed up the session.

Thanks

Ben