[SOLVED] saving an array into a session var

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

[SOLVED] saving an array into a session var

Post 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
Last edited by batfastad on Mon Jul 04, 2005 5:10 pm, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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']);

User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post 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
Post Reply