Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting objects in the session 4

Status
Not open for further replies.

gregmosu

Programmer
Joined
Jul 8, 2002
Messages
117
Location
US
Is it possible to store an object in the session? something like this...

My auth class:
<?php
class authenticate{
var $username;
var $password;
var $auth_level;
var $authenticated=false;
var $login_date;
var $id;
}
?>

The page where I store this in the session:
<?php
include &quot;authenticate.php&quot;;

session_start();

$auth = new authenticate();
$auth->username=&quot;Greg&quot;;
$auth->password=&quot;1234&quot;;
$auth->login_date=&quot;Today's Date&quot;;
$auth->auth_level=1;
$auth->id=1;

session_register(&quot;auth&quot;);
?>

This way I can store all the users attributes in an object, and then store that object in the session, and access them when I need them. This works very well when done with java, but I havent gotten it to work in php.. and from what I've read, I need to store each one these in the session individually. Does anyone know a way around this?

Thanks,
Greg
 
Deja vu...

You need to serialize the object then load it into $_SESSION, then unserialize it on the way out. You can read the details at...


but seeing as this question is really familar... I'm guessing that you want more details... first of all, what version of PHP are you using? If it's 4.3 then this should work...
Code:
<?php
session_start();

$auth = new authenticate();        
$auth->username=&quot;Greg&quot;;
$auth->password=&quot;1234&quot;;
$auth->login_date=&quot;Today's Date&quot;;
$auth->auth_level=1;
$auth->id=1;

$store_auth = serialize($auth);
$_SESSION[&quot;auth&quot;] = $store_auth;
?>

then on the receiving page
<?php
session_start();
$stored_auth = $_SESSION[&quot;auth&quot;];
$auth = unserialize($stored_auth);
?>
g'luck

-Rob
 
Thanks again Rob! I think you basically answered this
question in another post of mine.. only I was asking
about requests in that one! :)
 
With the newer versions of PHP, you don't have to serialize or unserialize a class to make it usable by the session mechanism. You only have to assign the class to a session array element.

The gotcha is that PHP must have the definition of the class available before you invoke session_start().

Here's some test code that works with PHP 4.3.2:

test_class_in_session.inc:
Code:
<?php
class foo
{
   var $foo, $bar, $fubar;
	
   function foo()
   {
      $this->foo = 'foo';
      $this->bar = 'bar';
      $this->fubar = array ('f', 'u', 'b', 'a', 'r');
   }
	
   function show_all ()
   {
      print '<pre>';
      print $this->foo . &quot;\n&quot;;
      print $this->bar . &quot;\n&quot;;
      print_r ($this->fubar);
      print &quot;\n</pre>&quot;;
   }
}
?>


test_class_in_session.php:
Code:
<?php
include ('test_class_in_session.inc');

session_start();

print '<html><body>The class &quot;foo&quot; in the session:<br>';

$_SESSION['theclass'] = new foo;

print '<pre>';
print_r ($_SESSION['theclass']);
print '</pre></body></html>';
?>


test_class_in_session2.php:
Code:
<?php
include ('test_class_in_session.inc');

session_start();

print '<html><body>Invoking a method of the class in the session array:<br><pre>';

$_SESSION['theclass']->show_all();

print '</pre></body></html>';
?>


Invocation of test_class_in_session.php returns:
Code:
The class &quot;foo&quot; in the session:


foo Object
(
    [foo] => foo
    [bar] => bar
    [fubar] => Array
        (
            [0] => f
            [1] => u
            [2] => b
            [3] => a
            [4] => r
        )
)

Then pointing the browser to test_class_in_session2.php gets me:
Code:
Invoking a method of the class in the session array:


foo
bar
Array
(
    [0] => f
    [1] => u
    [2] => b
    [3] => a
    [4] => r
)


As I said, the class should be defined before the call of session_start(). Otherwise, PHP will barf.


Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks for that tip sleipnir... they keep packing PHP with so many updates it's hard to keep up with them.
 
Thanks sleipnir214 I'll look into that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top