Use of session variables

Use of session variables

am 13.01.2008 23:40:52 von plenty900

Hi all,

I was curious if one can put an array in a session variable.
I want to store some search results in such a variable,
so it would be an array of objects. However I don't know
if that was even the intended purpose of a session variable,
and I also don't know how long session variables are held,
or if there is a maximum size for a session variable.
Can anyone explain?

Thanks for any info...

Re: Use of session variables

am 14.01.2008 00:01:53 von thyb0

plenty900@yahoo.com wrote:
> Hi all,
>
> I was curious if one can put an array in a session variable.
> I want to store some search results in such a variable,
> so it would be an array of objects. However I don't know
> if that was even the intended purpose of a session variable,
> and I also don't know how long session variables are held,
> or if there is a maximum size for a session variable.
> Can anyone explain?
>
> Thanks for any info...

Of course it's possible, sessions are stored server-side and there's
basically no limitation.



It's just a bidimensional array.

-thib´

PS you can define a key for the array in the $_SESSION as well.

Re: Use of session variables

am 14.01.2008 00:06:30 von Paul Lautman

plenty900@yahoo.com wrote:
> Hi all,
>
> I was curious if one can put an array in a session variable.
> I want to store some search results in such a variable,
> so it would be an array of objects. However I don't know
> if that was even the intended purpose of a session variable,
> and I also don't know how long session variables are held,
> or if there is a maximum size for a session variable.
> Can anyone explain?
>
> Thanks for any info...

The manual can explain!
Try typing
php session objects
into Google and clicking the "I'm Feeling Lucky" button

Re: Use of session variables

am 14.01.2008 00:35:56 von Peter Pei

did you test be4 u ask?

Re: Use of session variables

am 14.01.2008 00:36:24 von Peter Pei

of course, why not,

Re: Use of session variables

am 14.01.2008 00:38:17 von Peter Pei

1)your sample won't work, at least not with php's current shape.
2) Of course it's , is it possible, or a definite yes. be precise

Re: Use of session variables

am 14.01.2008 01:58:09 von thyb0

Peter Pei wrote:
> 1)your sample won't work, at least not with php's current shape.
> 2) Of course it's , is it possible, or a definite yes. be precise
>

1) Ah?

session_start();
$_SESSION[] = array('why', 'wouldn\'t', 'this', 'work?');
echo '

' . print_r($_SESSION, true) . '
';
?>

outputs:

Array
(
[0] => Array
(
[0] => why
[1] => wouldn't
[2] => this
[3] => work?
)

)

Well, I must admit, I got a notice here:
Notice: Unknown: Skipping numeric key 0. in Unknown on line 0

By defining a non-numeric key for the array, it doesn't display this notice,
so I guess $_SESSION have special rules (I'm not that familiar with it)
until a 'normal' array will accept this code very well (I don't see any
glitch in $_SESSION neither).

2) I'm sorry, I must have missed something. I'm aware my English is not
perfect at all, maybe the literal translation hasn't the same meaning in
French and in English, or whatever; I'm frustrated I don't get what you
mean. Anyway, what I meant is "Definitely yes, it's possible to use arrays
in sessions".

-thib´

Re: Use of session variables

am 14.01.2008 02:04:27 von Peter Pei

on point 1, you are getting closer

Re: Use of session variables

am 14.01.2008 02:18:51 von thyb0

Peter Pei wrote:
> on point 1, you are getting closer

Hum.. I did explain exactly what the OP was asking. If you want plain
example of what I just said, here it is:

session_start();
$_SESSION['some_array'] = array('why', 'wouldn\'t', 'this', 'work?');
echo '

' . print_r($_SESSION, true) . '
';
?>
-----
Array
(
[some_array] => Array
(
[0] => why
[1] => wouldn't
[2] => this
[3] => work?
)

)


String key, thus no notice. Is something wrong with that?
*scratches his head*

-thib´

Re: Use of session variables

am 14.01.2008 02:21:53 von Peter Pei

string key is fine, I meant u were closer on the number key thing

Re: Use of session variables

am 14.01.2008 02:44:24 von thyb0

Peter Pei wrote:
> string key is fine, I meant u were closer on the number key thing

O-kay
Did some little search then, and found the logical reason: hellish globals
strike again. Even if register_global is off, PHP throws this notice simply
because of variables naming limitations (in this case it cannot start with
numbers).

As far as we don't register globals, I think we can simply @ignore this
notice and sleep well (correct me if there's something else).

Not talking about portability, of course.

Thanks for making me discovering it out ;).

-thib´

Re: Use of session variables

am 14.01.2008 02:50:13 von Jerry Stuckle

thib´ wrote:
> Peter Pei wrote:
>> string key is fine, I meant u were closer on the number key thing
>
> O-kay
> Did some little search then, and found the logical reason: hellish
> globals strike again. Even if register_global is off, PHP throws this
> notice simply because of variables naming limitations (in this case it
> cannot start with numbers).
>

Yep, it's a minor annoyance, but I can understand it. Maybe this
restriction will be removed some day when they completely get rid of
register_globals :-)

> As far as we don't register globals, I think we can simply @ignore this
> notice and sleep well (correct me if there's something else).
>

Probably could, but I personally don't like it. But then I always have
a name for anything I put in the $_SESSION variable anyway, so I can get
it back easily.

> Not talking about portability, of course.
>
> Thanks for making me discovering it out ;).
>
> -thib´
>


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Use of session variables

am 14.01.2008 02:50:45 von Peter Pei

right on! you got the reason. but unfortunately it won't work regardless. to
me, this is a bug in the language. if register_global turned off, numbers
should be allowed, however in reality php simply skips the assignment, and
this mess up yr subsequent pages - session_start won't be able to bring back
the indexed array element, although it appears to be fine on the same page

Re: Use of session variables

am 14.01.2008 02:52:31 von Peter Pei

his suggestion won't work. I see this as a bug.

Re: Use of session variables

am 14.01.2008 11:08:10 von Jonas Werres

> Thanks for any info...

Yes, it is serialized automatically. Which directly leads to the question:
Can I store objects in sessions?
Yes, you can, but you have to be sure, that the class definition is
included/includeable by autoload ON EVERY PAGE THE SESSION IS LOADED.
Yes, loaded. Not every page, the object is used but on every page the
session is loaded, because in other cases, the object will break when
unserialising.

Re: Use of session variables

am 14.01.2008 14:33:11 von Michael Fesser

..oO(thib´)

>Peter Pei wrote:
>> string key is fine, I meant u were closer on the number key thing
>
>O-kay
>Did some little search then, and found the logical reason: hellish globals
>strike again. Even if register_global is off, PHP throws this notice simply
>because of variables naming limitations (in this case it cannot start with
>numbers).

Correct.

>As far as we don't register globals, I think we can simply @ignore this
>notice and sleep well (correct me if there's something else).

The notice "Skipping numeric key 0 ..." also means that these variables
are _not_ stored in the session file, hence you cannot ignore it.
Currently $_SESSION acts as an associative array only, so no numeric
keys are allowed.

Micha

Re: Use of session variables

am 14.01.2008 14:34:41 von Michael Fesser

..oO(Peter Pei)

>of course, why not,

Why what? Your quoting sucks!

Micha

Re: Use of session variables

am 14.01.2008 22:55:26 von thyb0

Michael Fesser wrote:
> The notice "Skipping numeric key 0 ..." also means that these variables
> are _not_ stored in the session file, hence you cannot ignore it.
> Currently $_SESSION acts as an associative array only, so no numeric
> keys are allowed.
>
> Micha

Thanks for the correction, that's definitely good to know.

-thib´

Re: Use of session variables

am 15.01.2008 04:04:52 von Aaron Saray

On Jan 14, 4:08 am, Jonas Werres wrote:
> > Thanks for any info...
>
> Yes, it is serialized automatically. Which directly leads to the question:
> Can I store objects in sessions?
> Yes, you can, but you have to be sure, that the class definition is
> included/includeable by autoload ON EVERY PAGE THE SESSION IS LOADED.
> Yes, loaded. Not every page, the object is used but on every page the
> session is loaded, because in other cases, the object will break when
> unserialising.

Not to digress too much, but if storing objects in the session, it is
useful to be familiar with the magic sleep and wakeup methods. I've
made this mistake a few times - and caused a few issues with other
resources stored as references inside of the objects.