Note that there are some explanatory texts on larger screens.

plurals
  1. POFrom PHP4 to PHP5:session handler can't access global DB object
    primarykey
    data
    text
    <p>I am migrating a PHP app written many, many years ago in PHP 4.4 to a new host that runs PHP 5.3. The app was written using only the PEAR libraries and no other framework. I'm finding the following problem: I have in my main page this code:</p> <pre><code>require_once('DB.php'); //the PEAR DB package $db=DB::connect("mysql://usuario:@localhost/databasename"); if (DB::isError($db)) { die("MEEEEEPT"); } //Inicia sesión session_set_save_handler("Sesion_Open", "Sesion_Close", "Sesion_Read", "Sesion_Write", "Sesion_Delete", "Sesion_Clean"); session_name("misesion"); session_start(); </code></pre> <p>And then, the session handlers are:</p> <pre><code>function Sesion_Open() { global $db; if (DB::isConnection($db)) { return true; } else { return false; } } function Sesion_Close() { global $db; $db-&gt;disconnect(); return true; } function Sesion_Read($id) { global $db; $resultado=$db-&gt;query("SELECT datos FROM sesiones WHERE id='$id'"); $num=$resultado-&gt;numRows(); if ($num &gt; 0) { $row=$resultado-&gt;fetchRow(DB_FETCHMODE_ASSOC); $resultado-&gt;free(); return $row['datos']; } else { $resultado-&gt;free(); return ""; } } function Sesion_Write($id, $datos) { global $db; $datos=addslashes($datos); $num=$db-&gt;getOne("SELECT COUNT(id) FROM sesiones WHERE id='$id'"); if ($num &gt; 0) { $sql="UPDATE sesiones SET datos='$datos', tiempo=now() WHERE id='$id'"; } else { $sql="INSERT INTO sesiones (id, tiempo, datos) VALUES ('$id', now(), '$datos')"; } $resultado=$db-&gt;query($sql); if (DB::isError($resultado)) { return false; } else { return true; } } </code></pre> <p>If I try to save some data in the session, using, say:</p> <pre><code>$_SESSION['bkla']="Doremifa"; </code></pre> <p>It doesn't get saved: the DB sessions table remains empty, and I get an error in the log saying:</p> <pre><code> PHP Fatal error: Call to a member function getOne() on a non-object in sesiones.php on line 45 </code></pre> <p>Referring to the line in "Sesion_Write" where I try to do a SELECT on the table.</p> <p>From what I see, the "Sesion_Open" function can access correctly the $db object, but the others can't see it; the object doesn't seem to exist for them. What's going on? I haven't touched PHP in a few years, so I don't know if something changed between PHP4 and PHP5 to cause this, or there's something else.</p>
    singulars
    1. This table or related slice is empty.
    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