Problems using sessions with PHP class objects

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Problems using sessions with PHP class objects

Postby dgp0961 » 05. January 2007 22:33

I am having trouble with the following code example which uses sessions to store a PHP class object on one page and refers to that object on a second page. My simple example is listed below. When I run this via xampplite, the first page displays fine and creates the object, when I click on the Page 2 link, the second page loads, retrieves the object from the $_SESSION array, but generates the following error.

Object passed via $_SESSION

Fatal error: main() [<a
href='function.main'>function.main</a>]: The script
tried to execute a method or access a property of an
incomplete object. Please ensure that the class
definition &quot;Myclass&quot; of the object you are
trying to operate on was loaded _before_ unserialize()
gets called or provide a __autoload() function to load
the class definition in
C:\XAMPP\xampplite\htdocs\MyClassPage2.php on line 17



Any thoughts?



Myclass.class.php
PHP Code:
class Myclass
{
public $var;

function __construct()
{
$this->var = 'Hello from ';
}

function echo_var()
{
echo $this->var . $_SERVER['PHP_SELF'] . '<br />';
}
}

page1.php
PHP Code:
include './Myclass.class.php';
session_start();
if (isset($_SESSION['newobj'])) {
$newobj = $_SESSION['newobj'];
echo 'Object passed via $_SESSION<br />';
} else {
$newobj = new Myclass;
$_SESSION['newobj'] = $newobj;
echo 'Object created with <I>new</I><br />';
}
$newobj->echo_var();
echo '<a href="page2.php">Page 2</a>';

page2.php
PHP Code:
include './Myclass.class.php';
session_start();
if (isset($_SESSION['newobj'])) {
$newobj = $_SESSION['newobj'];
echo 'Object passed via $_SESSION<br />';
} else {
$newobj = new Myclass;
$_SESSION['newobj'] = $newobj;
echo 'Object created with <I>new</I><br />';
}
$newobj->echo_var();
echo '<a href="page1.php">Page 1</a>';
dgp0961
 
Posts: 4
Joined: 30. December 2006 18:20

Postby hoergen » 12. January 2007 08:37

That has nothing to do with buggy code. There is a unhappy configuration in php.ini

Change the session.save_path to something like

Code: Select all
session.save_path = "0;600;C:\Temp


dem hoergen
hoergen
 
Posts: 11
Joined: 12. January 2007 08:30


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 113 guests