Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Session Variable

Status
Not open for further replies.

rvr1

Programmer
Feb 6, 2004
16
NL
I hope this will be a basic question for some of you – it’s my first major PHP project and I am picking it up as I go along.

Please can someone explain to me how to store a dynamic number of variables so that they can recalled on future pages. i.e. I wish to mimic the functionality of a shopping cart on a very basic level.

I am familiar with PHP sessions and static session variables, however, don’t really understand how to tackle the problem where the number of variables required is unknown. Some kind of array session variable perhaps?

Any help would be greatly appreciated.
 
An array will do it. Particularly a multidimensional associative array.

You can also store objects in session variables, too. The "gotcha" is that the class which the object instantiates must be included in your code before your invokation of session_start().


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
You can store anything you want in a session variable, including arrays. For example:

Code:
<?
//in first program

start_session();

$test_array = array('1','12','abvd');

$_SESSION['ta'] = serialize($test_array);

?>

<?
//in the second program
start_session();

$test_array = unserialize($_SESSION['ta']);

?>

Read the PHP manual on serialize and unserialize.

Ken
 
Really? I just did a quick test..
Here's the source:
Code:
<?
session_start();
$t = array('1','123','abc');
$_SESSION['t'] = $t;
$_SESSION['s'] = serialize($t);
echo '<pre>';print_r($_SESSION);echo '</pre>';
?>

Here's the result with PHP Version 4.3.8
Code:
Array
(
    [t] => Array
        (
            [0] => 1
            [1] => 123
            [2] => abc
        )

    [s] => a:3:{i:0;s:1:"1";i:1;s:3:"123";i:2;s:3:"abc";}
)

Maybe that's an option that can be turned on in the INI file. BTW, I got the same results on two different machines.

Ken
 
I don't understand your test. If you serialize an array, you get a string. If you store that string in a session, the next script will correctly retrieve a sting.

Here's my test scripts:

[tt]test_session_class.php[/tt]:
Code:
<?php

class fubar
{
	var $barfu;
	
	function fubar()
	{
		$this->barfu = 3;
	}
}
?>

[tt]test_complex_session1.php[/tt]:
Code:
<?php
include ('test_session_class.php');

session_start();

$_SESSION['bar'] = array
(
	'a' => array
	(
		'b' => array
		(
			'c' => array
			(
				'd',
				'e',
				'f'
			)
		)
	)
);

$_SESSION['baz'] = new fubar();
$_SESSION['baz']->barfu = 1;

print '<html><body><a href="test_complex_session2.php">here</a></body></html>';
?>

[tt]test_complex_session2.php[/tt]:
Code:
<?php
include ('test_session_class.php');
session_start();
print '<pre>';

print_r ($_SESSION['bar']);
print_r ($_SESSION['baz']);

?>

When I point my browser to test_complex_session1.php, it returns a simple link to test_complex_session2.php.

When I click on that link, test_complex_session2.php returns:

[tt]Array
(
[a] => Array
(
=> Array
(
[c] => Array
(
[0] => d
[1] => e
[2] => f
)

)

)

)
fubar Object
(
[barfu] => 1
)[/tt]

The arrays and objects are handed back and forth from one script to anther with no need for invocation of serialize() or unserialize(). This works with every version of PHP 4.3.x I've ever run (includuing 4.3.8) and with PHP 5.0.1.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Ok.

I got it. I looked at the session temp file after running your example and it contained the serialized strings.

Code:
bar|a:1:{s:1:"a";a:1:{s:1:"b";a:1:{s:1:"c";a:3:{i:0;s:1:"d";i:1;s:1:"e";i:2;s:1:"f";}}}}baz|O:5:"fubar":1:{s:5:"barfu";i:1;}

I was getting confused with the output of the print_r. I was expecting it to show the raw contents of the session variable, not what PHP thinks I want to see. [sad]

Now I can go through my code and remove my serialize/unserialize pairs on session variables.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top