Use of session variables
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
plenty900 [at] 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.
<?php $_SESSION[] = $my_array; ?>
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
plenty900 [at] 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
did you test be4 u ask?
Re: Use of session variables
of course, why not,
Re: Use of session variables
1)your sample won't work, at least not with php's current shape.
2) Of course it's <possible>, is it possible, or a definite yes. be precise
Re: Use of session variables
Peter Pei wrote:
> 1)your sample won't work, at least not with php's current shape.
> 2) Of course it's <possible>, is it possible, or a definite yes. be precise
>
1) Ah?
<?php
session_start();
$_SESSION[] = array('why', 'wouldn\'t', 'this', 'work?');
echo '<pre>' . print_r($_SESSION, true) . '</pre>';
?>
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
on point 1, you are getting closer
Re: Use of session variables
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:
<?php
session_start();
$_SESSION['some_array'] = array('why', 'wouldn\'t', 'this', 'work?');
echo '<pre>' . print_r($_SESSION, true) . '</pre>';
?>
-----
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
string key is fine, I meant u were closer on the number key thing
Re: Use of session variables
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 [at] 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
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
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 [at] 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 [at] attglobal.net
==================
Re: Use of session variables
his suggestion won't work. I see this as a bug.
Re: Use of session variables
> 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
..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 [at] 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
..oO(Peter Pei)
>of course, why not,
Why what? Your quoting sucks!
Micha
Re: Use of session variables
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
On Jan 14, 4:08 am, Jonas Werres <jo... [at] example.org> 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.