php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #17436 objects don't persist in sessions
Submitted: 2002-05-26 13:49 UTC Modified: 2003-03-26 00:58 UTC
Votes:12
Avg. Score:4.9 ± 0.3
Reproduced:12 of 12 (100.0%)
Same Version:2 (16.7%)
Same OS:1 (8.3%)
From: ndp at mac dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.2.1 OS: MacOS X 10.1.4
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ndp at mac dot com
New email:
PHP Version: OS:

 

 [2002-05-26 13:49 UTC] ndp at mac dot com
I have not been able to get objects to "come back to life" 
since version 4.1. I have read (and understand) all the new 
superglobals and security issues, and I am convinced it is 
not that (although for a while I was blaming my 
misunderstanding). The message php returns is that the 
class is not defined-- but it is.

The following code shows the problem. You can see that 
regular variables come back on the session page, but the 
simple object just causes an error. 

Here is the output:

------

Session variable set.
Session message made it: Variable made it.
Fatal error: The script tried to execute a method or access 
a property of an incomplete object. Please ensure that the 
class definition test_class of the object you are trying to 
operate on was loaded _before_ the session was started in /
Users/ndp/Sites/php/session_test.php on line 27

-----

Here is the source code named session_test.php:

-----

<?

class test_class
{
	var $m;
	
	function test_class()
	{
		$this->m = "";
	}
	
	function set_m($in_m)
	{
		$this->m = $in_m;
	}
	
	function get_m()
	{
		return $this->m;
	}
}

if (isset($_SESSION["m"]))
{
	print "Session variable set.<br />";
	print "Session message made it: ".$_SESSION["message"]."<
br />";
	print "Value = ".$_SESSION["m"]->get_m()."<br />";
}
else
{
	session_start();
	
	$_SESSION["message"] = "Variable made it.";
	$_SESSION["m"] = new test_class();
	$_SESSION["m"]->set_m("YES");
}


?>

<form action=session_test.php method=get>
	<input type=submit>
<form>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-01 12:14 UTC] markonen@php.net
Reproduced here on OS X. mod_files can write the session 
files ok, but doesn't seem to be able to read them. Digging 
into this.
 [2002-06-01 12:32 UTC] markonen@php.net
Um, sorry, I reproduced a different bug. Sessions are not 
working at all on my box. Looking into it.
 [2002-06-19 05:44 UTC] twocandles3000 at hotmail dot com
i suppose you have session auto_start disabled, don't you? since class declaration must appear before unserializing an object, this code won't work because of the unserialization occurs before the class declaration if auto_start is set.
 [2002-06-20 14:10 UTC] ndp at mac dot com
Yes, the issue is indeed session auto_start. (I put a 
comment in about this, but don't see it now.) 

My recommendation was that the error message could be 
changed to indicate what is going on, as this was pretty 
hard to track down. The upgrade came with a new php.ini 
file, and I trusted it-- therefore it seemed like the new 
version was broken, when it wasn't. 

The error message could be something like "session objects 
are not compatible with session_auto_start setting". That 
would help you fix the problem.

I put notes where I could on the php.net site, but it seems 
like someone else will undoubtedly run into the same 
problem and have the same difficulty diagnosing it.

Andy
 [2002-09-16 14:55 UTC] derick@php.net
It should be documented that session autostart causes this problem (read latest posting by user).

Derick
 [2003-03-26 00:58 UTC] rasmus@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 [2003-04-26 18:26 UTC] iain at lexingtonwarner dot com
Hi, is it possible to have something like "Class definitions must appear prior to object being unserialized" as someone has written here, in the error message? That was the comment that helped me out of my little hole :-)
 [2003-06-30 12:54 UTC] developer at yecc dot net
On my computer I have php 4.0.1
I use serialize function after I create a object of a class
and then error does not occured.
I think If use serialize can solve problem of this error.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 17 21:01:27 2024 UTC