Jump to content

Undefined const error: can't access constant var from set_save_handler write api


pkrish

Recommended Posts

Hi All,

 

I am using PHP 5.3 and storing php session data to the database using session.set_save_handler callbacks. I see that the write method, strangely treats the Class constant as undefined. All the other methods seem to work fine with the class constant. Any reason why this would happen? Please see code below. I get an error "Use of undefined constant db_table in Sessions.php in line". This seems to be happening only with the write api. Can someone please help?

 

Thanks

 

 

class Sessions {

//var $life_time;

var $id;

var $data;

const db_table = 'php_mysql_session';

function PHPSessions() {

// Read the maxlifetime setting from PHP

//$this->life_time = get_cfg_var("session.gc_maxlifetime");

 

$this->id = session_id();

$this->data = '';

 

// Register this object as the session handler

session_set_save_handler(

array( &$this, "_open" ),

array( &$this, "_close" ),

array( &$this, "_read" ),

array( &$this, "_write" ),

array( &$this, "_destroy") ,

array( &$this, "_gc" )

);

 

}

function _open ()

// open the session.

{

Zend_Log::log("opening connection");

global $_sess_db;

Zend_Log::log("session db ".$_sess_db);

if ($_sess_db = mysql_connect(DB_HOST_READ, DB_USER, DB_PASS)) {

return mysql_select_db('TEST_SESSION', $_sess_db);

}

// If there is a failure return false

return FALSE;

 

} // open

 

 

function _close ()

// close the db connection here

{

global $_sess_db;

return mysql_close($_sess_db);

 

} // close

 

function _read ($id)

// read any data for this session.

{

 

 

    global $_sess_db;

    Zend_Log::log("Session db ".$_sess_db);

//$id = mysql_real_escape_string(session_id());

    Zend_Log::log("Session Id :" .$id);

 

$sql = "SELECT * FROM ". self::db_table ."

WHERE session_id = '$id'";

Zend_Log::log("SQL IS : ".$sql);

 

    $data = '';

$result = mysql_query($sql);

$a = mysql_num_rows($result);

Zend_Log::log("Record count ".$a);

 

if($a > 0) {

Zend_Log::log("Record Exists!");

$row = mysql_fetch_assoc($result);

$data = $row['session_data'];

Zend_Log::log("Fetching session Data ".$data);

return $data;

}

else

{

Zend_Log::log("No Data ".$data);

return '';

}

 

 

 

}

 

function _write ($id, $data)

// write session data to the database.

{

global $_sess_db;

 

 

$newid = mysql_real_escape_string($id);

 

$newdata = mysql_real_escape_string($data);

 

$sql = "REPLACE ". self::db_table ." (session_id,user_id,session_data,date_created) VALUES('$newid', '$userid', '$newdata', 'NOW()')";

 

$rs = mysql_query($sql);

 

return TRUE;

 

 

} // write

 

 

function _destroy ($id)

// destroy the specified session.

{

    Zend_Log::log("Destroy method ");

return TRUE;

 

} // destroy

 

function _gc ($max_lifetime)

// perform garbage collection.

{

    Zend_Log::log("GC Method");

return TRUE;

 

} // gc

 

function _destruct ()

// ensure session data is written out before classes are destroyed

// (see http://bugs.php.net/bug.php?id=33772 for details)

{

@session_write_close();

 

} // __destruct

 

// ****************************************************************************

} // end class

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.