Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Global variable not being imported into session handler
    primarykey
    data
    text
    <p>I'm using ADOdb. For some reason, the actual <code>$db</code> is not getting imported in the 'write' function.</p> <p>The function is intended to import the <code>$db</code>'s actual value. Instead, it's assigning an empty value to <code>$db</code>:</p> <pre><code>&lt;?php // load ADODB class include(DIR_WS_CLASSES . "adodb5/adodb.inc.php"); $db = NewADOConnection(DB_TYPE); $db-&gt;Connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE); class SessionManager { var $life_time; function SessionManager(){ global $db; // Read the maxlifetime setting from PHP $this-&gt;life_time = get_cfg_var("session.gc_maxlifetime"); // Register this object as the session handler session_set_save_handler(array(&amp;$this, "open"), array(&amp;$this, "close"), array(&amp;$this, "read"), array(&amp;$this, "write"), array(&amp;$this, "destroy"), array(&amp;$this, "gc")); } function open($save_path, $session_name){ global $sess_save_path; global $db; $sess_save_path = $save_path; return true; } function close(){ global $db; return true; } function read($id){ global $db; // Set empty result $data = ''; // Fetch session data from the selected database $time = time(); $newid = $db-&gt;qstr($id, get_magic_quotes_gpc()); $sql = "SELECT session_data FROM sessions WHERE session_id = $newid AND expires &gt; $time"; $rs = $db-&gt;Execute($sql) or die($db-&gt;ErrorMsg()); $a = $rs-&gt;RecordCount(); if($a &gt; 0){ $data = $rs-&gt;fields['session_data']; } return $data; } function write($id, $data){ global $db; // Build query $time = time() + $this-&gt;life_time; $newid = $db-&gt;qstr($id, get_magic_quotes_gpc()); $newdata = $db-&gt;qstr($data, get_magic_quotes_gpc()); $sql = "REPLACE sessions (session_id, session_data, expires) VALUES($newid, $newdata, $time)"; $rs = $db-&gt;Execute($sql) or die($db-&gt;ErrorMsg()); return TRUE; } function destroy($id){ global $db; // Build query $newid = $db-&gt;qstr($id, get_magic_quotes_gpc()); $sql = "DELETE FROM sessions WHERE session_id = $newid"; $db-&gt;Execute($sql) or die($db-&gt;ErrorMsg()); return TRUE; } function gc(){ // Garbage Collection global $db; // Build DELETE query. Delete all records that passed expiration time $sql = "DELETE FROM sessions WHERE expires &lt; UNIX_TIMESTAMP()"; $db-&gt;Execute($sql) or die($db-&gt;ErrorMsg()); // Always return TRUE return true; } } // initialize session $sess = new SessionManager(); session_start(); ?&gt; </code></pre> <p>Why is the actual <code>$db</code> not imported in the 'write' function?</p> <p>How can I fix the problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload