Session with objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maxi
Forum Newbie
Posts: 10
Joined: Tue Apr 29, 2003 3:46 pm
Location: Buenos Aires, Argentina

Session with objects

Post by maxi »

I had experienced some troubbles setting an object in a variable session. I tried to do:

page1.php
<?php
session_start();

$arbitrer = new COM("Arbitrer.1") or die("I can't create Arbitrer");
$rc = $arbitrer->Logon( $_REQUEST ["name"], $_REQUEST ["password"], $var );
if ( $rc != 0 ){
// function error
...
}
$_SESSION['arbitrer'] = $arbitrer;

header("Location: page2.php");

// That's work right

------------------------------------
page2.php

<?php
session_start();
if ( is_null( $_SESSION['arbitrer'] ) ) {
print "I can't get arbitrer";
}

//Always print the message because abitrer is null


What am i doing wrong??? Anyone has any idea??? I'm driving crazy with this problem!!! :cry:
maxi
Forum Newbie
Posts: 10
Joined: Tue Apr 29, 2003 3:46 pm
Location: Buenos Aires, Argentina

Post by maxi »

I forgot to say I'm working with Windows NT Workstation 4.0 (SP6) Apache 1.3.7 (I've tried with Apache 2) and PHP 4.3.2 RC2
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

maxi
Forum Newbie
Posts: 10
Joined: Tue Apr 29, 2003 3:46 pm
Location: Buenos Aires, Argentina

Post by maxi »

I've just tried with serialize() and unserialize() and it didn't work.
I lost the reference of the object within pages using $_SESSION independently if i use serialize() or not.
maxi
Forum Newbie
Posts: 10
Joined: Tue Apr 29, 2003 3:46 pm
Location: Buenos Aires, Argentina

Post by maxi »

I'm trying to store a COM object in a session but I've just read that php can't store a COM object in a session. It's true? Is there any patch to fix it?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

php (by default) stores sessions in flat files. Everything is casted to string before saved. But that doesn't work for objects esp. not for COM-objects. If you're lucky the COM-object itself implements some kind of persistence-interface, so you can invoke it when serializing your data.
maxi
Forum Newbie
Posts: 10
Joined: Tue Apr 29, 2003 3:46 pm
Location: Buenos Aires, Argentina

Post by maxi »

What I have mentioned about storing COM object in a session I got it from the manual of PHP in "FAQ: Frequently Asked Questions" -> "PHP and COM"

"4. Can I store a COM object in a session ?

No, you can't. COM instances are treated as resources and therefore they are only available in a single script's context. "
(http://www.php.net/manual/en/faq.com.php#faq.com.q4)

So, when i store a com-object in a session variable doing:
$_SESSION("myObject") = serialize($myObject);

I can't restore the object (in another page) doing:
$myObject = unserialize($_SESSION("myObject");
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

that's correct.
But some COM-components implement their own persistence mechanism, that you might call to store the object. That's a rather long shot but the best I can offer ;)
maxi
Forum Newbie
Posts: 10
Joined: Tue Apr 29, 2003 3:46 pm
Location: Buenos Aires, Argentina

Post by maxi »

Ok, thanks you very much.
I'll see what can i do...
Post Reply