Connecting Tech Pros Worldwide Forums | Help | Site Map

Use of session variables

plenty900@yahoo.com
Guest
 
Posts: n/a
#1: Jan 13 '08
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...

=?ISO-8859-1?Q?thib=B4?=
Guest
 
Posts: n/a
#2: Jan 13 '08

re: Use of session variables


plenty900@yahoo.com wrote:
Quote:
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.
Paul Lautman
Guest
 
Posts: n/a
#3: Jan 13 '08

re: Use of session variables


plenty900@yahoo.com wrote:
Quote:
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


Peter Pei
Guest
 
Posts: n/a
#4: Jan 13 '08

re: Use of session variables


did you test be4 u ask?
Peter Pei
Guest
 
Posts: n/a
#5: Jan 13 '08

re: Use of session variables


of course, why not,
Peter Pei
Guest
 
Posts: n/a
#6: Jan 13 '08

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

=?ISO-8859-1?Q?thib=B4?=
Guest
 
Posts: n/a
#7: Jan 14 '08

re: Use of session variables


Peter Pei wrote:
Quote:
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ī
Peter Pei
Guest
 
Posts: n/a
#8: Jan 14 '08

re: Use of session variables


on point 1, you are getting closer
=?ISO-8859-1?Q?thib=B4?=
Guest
 
Posts: n/a
#9: Jan 14 '08

re: Use of session variables


Peter Pei wrote:
Quote:
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ī
Peter Pei
Guest
 
Posts: n/a
#10: Jan 14 '08

re: Use of session variables


string key is fine, I meant u were closer on the number key thing
=?ISO-8859-1?Q?thib=B4?=
Guest
 
Posts: n/a
#11: Jan 14 '08

re: Use of session variables


Peter Pei wrote:
Quote:
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ī
Peter Pei
Guest
 
Posts: n/a
#12: Jan 14 '08

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

Jerry Stuckle
Guest
 
Posts: n/a
#13: Jan 14 '08

re: Use of session variables


thibī wrote:
Quote:
Peter Pei wrote:
Quote:
>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 :-)
Quote:
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.
Quote:
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
==================

Peter Pei
Guest
 
Posts: n/a
#14: Jan 14 '08

re: Use of session variables


his suggestion won't work. I see this as a bug.
Jonas Werres
Guest
 
Posts: n/a
#15: Jan 14 '08

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.
Michael Fesser
Guest
 
Posts: n/a
#16: Jan 14 '08

re: Use of session variables


..oO(thibī)
Quote:
>Peter Pei wrote:
Quote:
>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.
Quote:
>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
Michael Fesser
Guest
 
Posts: n/a
#17: Jan 14 '08

re: Use of session variables


..oO(Peter Pei)
Quote:
>of course, why not,
Why what? Your quoting sucks!

Micha
=?ISO-8859-1?Q?thib=B4?=
Guest
 
Posts: n/a
#18: Jan 14 '08

re: Use of session variables


Michael Fesser wrote:
Quote:
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ī
Aaron Saray
Guest
 
Posts: n/a
#19: Jan 15 '08

re: Use of session variables


On Jan 14, 4:08 am, Jonas Werres <jo...@example.orgwrote:
Quote:
Quote:
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.
Closed Thread