For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > March 2006 > storing object in session









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author storing object in session
bmaster

2006-03-12, 6:56 pm

Hi there,

I'm trying to figure out how to store an object in session. To try this,
I have apache2 and php5 running on my windows machine. I used the 2
files below. First i run test.php, which creates the object, puts some
data in it, and stores it in the session. test2.php retrieves the object
from the session and prints the data. Works fine on my windows machine.
However, when I upload these 2 files to my hosting provider, then it
does not work anymore. I get no errors, but the object seems to be
empty. The provider is running php4.3 ... Can someone help me find a way
to fix this? Thanks!!


-------- test.php -----------------------------------------------------
<?php
class claUser{
var $firstname;
var $lastname;
}

session_start(); // Start Session

$user = new claUser;

$user->firstname = "abc";
$user->lastname = "def";

$_SESSION['user'] = serialize($user);


?>

-------- test2.php -----------------------------------------------------
<?php
class claUser{
var $firstname;
var $lastname;
}

session_start(); // Start Session


$user = new claUser;

if (isset($_SESSION['user']))
{
$user = unserialize($_SESSION['user']);
}

print $user->firstname;


?>
ZeldorBlat

2006-03-12, 6:56 pm


bmaster wrote:
> Hi there,
>
> I'm trying to figure out how to store an object in session. To try this,
> I have apache2 and php5 running on my windows machine. I used the 2
> files below. First i run test.php, which creates the object, puts some
> data in it, and stores it in the session. test2.php retrieves the object
> from the session and prints the data. Works fine on my windows machine.
> However, when I upload these 2 files to my hosting provider, then it
> does not work anymore. I get no errors, but the object seems to be
> empty. The provider is running php4.3 ... Can someone help me find a way
> to fix this? Thanks!!
>
>
> -------- test.php -----------------------------------------------------
> <?php
> class claUser{
> var $firstname;
> var $lastname;
> }
>
> session_start(); // Start Session
>
> $user = new claUser;
>
> $user->firstname = "abc";
> $user->lastname = "def";
>
> $_SESSION['user'] = serialize($user);
>
>
> ?>
>
> -------- test2.php -----------------------------------------------------
> <?php
> class claUser{
> var $firstname;
> var $lastname;
> }
>
> session_start(); // Start Session
>
>
> $user = new claUser;
>
> if (isset($_SESSION['user']))
> {
> $user = unserialize($_SESSION['user']);
> }
>
> print $user->firstname;
>
>
> ?>


Get rid of the serialize/unserialize stuff, then read this:

<http://groups.google.com/group/alt....7db1d734b24c501>

bmaster

2006-03-12, 6:56 pm

ZeldorBlat wrote:
> bmaster wrote:
[color=darkred]
>
> Get rid of the serialize/unserialize stuff, then read this:
>
> <http://groups.google.com/group/alt....7db1d734b24c501>
>


I tried it with and without the serialize stuff, but neither worked. But
thanks for the link tho, session.auto_start is enabled indeed it seems.
I mailed my hosting provider to disable it, hope it helps. If not, I'll
be back :-) Thanks!
bmaster

2006-03-16, 6:56 pm

ZeldorBlat wrote:
>
> Get rid of the serialize/unserialize stuff, then read this:
>
> <http://groups.google.com/group/alt....7db1d734b24c501>


The provider adjusted the autostart session setting, but I still have
the same problem... Do you have any other ideas that I can try?

Thanks !!




ZeldorBlat

2006-03-16, 6:56 pm


bmaster wrote:
> ZeldorBlat wrote:
>
> The provider adjusted the autostart session setting, but I still have
> the same problem... Do you have any other ideas that I can try?
>
> Thanks !!


Make sure error reporting is enabled (either by displaying the errors
or writing them to a log) and turned up. Run it again and see if you
get any messages.

C.

2006-03-17, 7:55 am

There are good reasons for using serialize/unserialize. However there
are complications using objects with sessions too as alluded to in the
thread ZB points to.

Your code looks fine bmaster - it should work.

The problem is that the session doesn't seem to get reloaded (maybe it
didn't get saved).
Check you got a session cookie. If possible, try to find the session
files, or use a different session handler.

C.

bmaster

2006-03-17, 6:56 pm

ZeldorBlat wrote:
> bmaster wrote:
>
> Make sure error reporting is enabled (either by displaying the errors
> or writing them to a log) and turned up. Run it again and see if you
> get any messages.
>


display_errors is on, but I see no errors...
bmaster

2006-03-17, 6:56 pm

C. wrote:
> There are good reasons for using serialize/unserialize. However there
> are complications using objects with sessions too as alluded to in the
> thread ZB points to.
>
> Your code looks fine bmaster - it should work.
>
> The problem is that the session doesn't seem to get reloaded (maybe it
> didn't get saved).
> Check you got a session cookie. If possible, try to find the session
> files, or use a different session handler.
>


I see the cookie called 'PHPSESSID' ... don't really know if I can see
what's in it.. ? If you say 'session files', do you mean the session
files on the server itself? Don't know if I have access to that... will
try that too later today. I'll get back with (hopefully) good results!
bmaster

2006-03-17, 6:56 pm

bmaster wrote:
> C. wrote:
>
> I see the cookie called 'PHPSESSID' ... don't really know if I can see
> what's in it.. ? If you say 'session files', do you mean the session
> files on the server itself? Don't know if I have access to that... will
> try that too later today. I'll get back with (hopefully) good results!


The PHPSESSID cookie is all I can see. Maybe this is normal, I don't
know. This is what phpinfo() tells me about the sessions: (sorry about
the layout)

Session Support enabled
Registered save handlers files user

Directive Local Value Master Value
session.auto_start Off On
session.bug_compat_42 Off Off
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 1000 1000
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off

Hope this can help.. ?
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com