arrays passed via session

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
rjwebgraphix
Forum Newbie
Posts: 11
Joined: Fri Mar 11, 2005 8:10 am

arrays passed via session

Post by rjwebgraphix »

Trying to figure out how an array is being passed from a session.

This is what I have and what I'm trying to do...

Code: Select all

if (isset($_GET['error']) or isset($_GET['missing']))
{
    foreach ($_SESSION as $key => $value) 
    { 
        $formfield[$key] = $value;  // converts session to $formfield
        unset ($_SESSION);         // Dumps the session data
    }
}
There is an array that is being passed back that looks like this...

$os_version['dos'] = "on";
$os_version['win98se'] = "off";
$os_version['win2k'] = "off";
$os_version['winme'] = "off";

Displaying the $formfield array looks like this...

Array ( [os_version] => Array ( [\'dos\'] => on ) )

How can I use this data brought from a session? The following does not work....

Code: Select all

if ($os_version['dos'] == "on")
{
    echo " Dos is on ";
}
I didn't think it would because the array is actually now $formfield['os_version'] I think.

Any help on using an array brought to page from a session would be helpful.

Thanks,

RJ
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Don't know what you actually want.. but there is no need to foreach loop through an array when you are copying one.

Code: Select all

$formfield = $_SESSION;
you can use multi-dimensional arrays:

Code: Select all

$array[0][0];
and unsetting $_SESSION is a big no-no.
rjwebgraphix
Forum Newbie
Posts: 11
Joined: Fri Mar 11, 2005 8:10 am

Post by rjwebgraphix »

Jenk wrote:and unsetting $_SESSION is a big no-no.
Please explain further. Had to for other complications, but maybe that was not the best thing and could have been accomplished a different way, but had a problem with information sticking around that shouldn't, so that was my fix.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

From the manual:
Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal.
http://www.php.net/manual/en/ref.session.php

:)
rjwebgraphix
Forum Newbie
Posts: 11
Joined: Fri Mar 11, 2005 8:10 am

Post by rjwebgraphix »

Jenk wrote:From the manual:
Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal.
http://www.php.net/manual/en/ref.session.php

:)
I see. I'll have to re-visit that then and probably just unset a bunch of individuals. (UGH)
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

you can also just unset one var on session using:

unset($_SESSION['varname']);

and i think the best way to destroy a session is using session_destroy();
Post Reply