Welcome Guest, Not a member yet? Register   Sign In
Multidimensional arrays in session lib

Is it possible to handle multidimensional arrays in the session lib ?

for example; $_SESSION['cart']['productID']['productName']

Can i access the key 'productName' anyway about the $this->session->userdata('productName') class ?

Getting a multidimensional value is something that isn't provided in native php so you need to create your own function. For the new From_validation library i have created/modified two functions which could be useful for the $_SESSION global too. Maybe they should be added to the common.php file?

When they are implemented you could do $this->session->userdata(‘cart[productID][productName]’) to get the value.

Why isnt there a way in the session library itself ?

There's often a case to handle some session data in an array (like the shopping cart)

Other ways so solve it ?

You can use the COOKIE global instead of the userdata method but with the SVN changes in the session library it's going to be more difficult because one of the changes is storing the session variables in a database instead of a cookie. I haven't checked the changes but i wonder how they are storing array values in the database.

[eluser]Pascal Kriete[/eluser]
[quote author="xwero" date="1223486360"]i wonder how they are storing array values in the database.[/quote]

of course Smile

The session array is not stored in the database. The documentation is a bit misleading. Only the ID, User Agent, TimeStamp, and IP Address are stored in the database. You still only have 4KB of space (due to cookies) to store data, which equates to about 2.5KB if you use encryption.

That said, the sessions are secure if you set encrypt_coo.... to TRUE. Adding the database makes them even more secure because the session_id is checked against the DB session_id, and the User Agent is checked. This prevents session fixation, and hijacking. I'm not recommending anything, because I could be wrong, but if you set the encryption keyword in your config to something only you know, this "salt" makes the cookie data so secure that nobody could possibly find out what it means, even if they had multiple values (because the encryption is true - not just algorithmic), so in essence you could store a CC# (Don't though).

Theme © iAndrew 2016 - Forum software by © MyBB