Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Help with Sessions

posted by ecropolis 8 years ago

I'm trying to build an array of tokens which are breadcrumbs for use with Amazon SimpleDB paged data. I am following your example of setting an associative array in a session. I have some code and it isn't working in that it breaks execution. $next page holds an integer and $token is a string. One of my questions is whether you really need to do a set vs. an add; of if an add on an unset array should work. And then below, am I messing up some syntax? I did figure out that the set isn't failing; it's setting something. I can't get any databack out; and from the print_r() I'm not sure if it's there are not since it's an object. I created a new page where I could try a simple test. The add method doesn't work when adding an array.

if(!isset($_SESSION['mediatoken'])) {
		fSession::set(
		'mediatoken',
		array(
			$nextpage => $token
		));
	} else {
		fSession::add(
		'mediatoken',
		array(
			$nextpage => $token
		));	
	}

Here's a print_r

Array
(
   [fSession::type] => normal
   [fSession::expires] => 1312483684
   [limit] => 25
   [thispage] => 11
   [mediatoken] => Array
       (
           [4] => CFSimpleXML Object
               (
               )

       )

)

second test

<? 
include_once('../inc/init.php');

$page = 1;
$token = 'token page 1';
fSession::set(
			'mediatoken',
			array(
				$page => $token
			));

$page = 2;
$token = 'token page 2';
fSession::add(
			'mediatoken',
			array(
				$page => $token
			));

// This will echo John
echo 'page 1 token:';
echo fSession::get('mediatoken[1]');
echo '<hr>';
echo 'page 2 token:';
echo fSession::get('mediatoken[2]');
?>

So fSession::add() takes whatever you give it and pushes it onto the end of an array in the session. If the array doesn't exist yet, it is created. So by calling add, you will probably get something like this, right?

array(
    'mediatoken' => array(
        '1' => array('token'),
        '2' => array(
            '2' => 'token'
        )
    )
)

Instead of that, I think you just want to reference the array indexes when setting:

include_once('../inc/init.php');
 
$page = 1;
$token = 'token page 1';
fSession::set("mediatoken[$page]", $token); 
$page = 2;
$token = 'token page 2';
fSession::set("mediatoken[$page]", $token); 
 
// This will echo John
echo 'page 1 token:';
echo fSession::get('mediatoken[1]');
echo '<hr>';
echo 'page 2 token:';
echo fSession::get('mediatoken[2]');

Just be sure to type cast your $page variable to an integer if you are accepting it as user input.

posted by wbond 8 years ago