Gossamer Forum
Home : Products : Gossamer Links : PHP Front End :

Help with class and object errors anyone?

Quote Reply
Help with class and object errors anyone?
Hello!

Can anyone help me out with this small session class I'm playing with. The below class is included into a file and then called with...

$SN = new Session;
$SN->start_session();

Code:
<?php
class Session {

function Session () {
$this->session_name = 'sessi0n';
$this->session_data = array();
$this->session_user_id = 'test';
}

function start_session () {
global $DB ;

# If we don't already have an open session via cookie
if (! $_COOKIE["$this->session_name"] ) {

$this->session_id = md5( microtime() );
$this->session_date = time() ;

$session = get_object_vars($this);
$info = var_export($session, true);

$ret = $DB->insert("Sessions", $info );

}
}
}
?>


When I run the above with the line "$ret = $DB->insert("Sessions", $info );" commented OUT it gives the following error notice in my log file:

[error] PHP Notice: Undefined index: sessi0n in /path/to/session.class.php on line XX (referring to $_COOKIE["$this->session_name"]). Do you know how to fix this error?

If I uncomment the line "$ret = $DB->insert("Sessions", $info );" it also returns the following error...

[Sun Nov 6 11:23:58 2005] [error] PHP Fatal error: Call to a member function on a non-object in /path/to/session.class.php on line XX referring to "$ret = $DB->insert("Sessions", $info );" - which I assume means it doesn't know about the $DB object that I'm trying to use. That DB object was created just before the session object so it does already exist.

Can anyone help?

Thanks,

Regan.

Last edited by:

ryel01: Nov 5, 2005, 2:30 PM
Quote Reply
Re: [ryel01] Help with class and object errors anyone? In reply to
You need to see whether the cookie is set using isset() to get rid of the first error.
Quote Reply
Re: [Hargreaves] Help with class and object errors anyone? In reply to
Thanks for that! Smile One down! Any idea on the "Call to a member function on a non-object" ? I thought making it a global would have worked but maybe globals don't pass into objects also? r.
Quote Reply
Re: [ryel01] Help with class and object errors anyone? In reply to
figured out the $DB error - needed to move a line up in the calling script so it loaded $DB_CFG beforehand.

r