简体   繁体   English

从PHP4到PHP5:会话处理程序无法访问全局数据库对象

[英]From PHP4 to PHP5:session handler can't access global DB object

I am migrating a PHP app written many, many years ago in PHP 4.4 to a new host that runs PHP 5.3. 我将许多年前用PHP 4.4编写的PHP应用程序迁移到运行PHP 5.3的新主机。 The app was written using only the PEAR libraries and no other framework. 该应用程序仅使用PEAR库而不使用其他框架编写。 I'm finding the following problem: I have in my main page this 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();

And then, the session handlers are: 然后,会话处理程序为:

function Sesion_Open() {

    global $db;

    if (DB::isConnection($db)) { return true; }
    else { return false; }
    }

function Sesion_Close() {

    global $db;

    $db->disconnect();
    return true;
    }

function Sesion_Read($id) {

    global $db;

    $resultado=$db->query("SELECT datos FROM sesiones WHERE id='$id'");
    $num=$resultado->numRows();
    if ($num > 0) {
        $row=$resultado->fetchRow(DB_FETCHMODE_ASSOC);
        $resultado->free();
        return $row['datos'];
        }
    else {
        $resultado->free();
        return "";
        }
    }

function Sesion_Write($id, $datos) {

    global $db;

    $datos=addslashes($datos);
    $num=$db->getOne("SELECT COUNT(id) FROM sesiones WHERE id='$id'");
    if ($num > 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->query($sql);
    if (DB::isError($resultado)) { return false; }
    else { return true; }
    }

If I try to save some data in the session, using, say: 如果我尝试在会话中保存一些数据,请使用:

$_SESSION['bkla']="Doremifa";

It doesn't get saved: the DB sessions table remains empty, and I get an error in the log saying: 它没有保存:数据库会话表仍然为空,并且我在日志中收到一条错误消息:

 PHP Fatal error:  Call to a member function getOne() on a non-object in sesiones.php on line 45

Referring to the line in "Sesion_Write" where I try to do a SELECT on the table. 参考“ Sesion_Write”中的行,我尝试在表上执行SELECT。

From what I see, the "Sesion_Open" function can access correctly the $db object, but the others can't see it; 从我所看到的,“ Sesion_Open”函数可以正确访问$ db对象,但其他人看不到它。 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. 几年来我还没有接触过PHP,所以我不知道PHP4和PHP5之间是否发生了某些变化而导致此问题,或者还有其他问题。

Ah, found the answer: 啊,找到答案了:

Strange behaviour migrating sessions with callbacks from php4 to php5 带有php4到php5回调的奇怪行为迁移会话

You need to either call session_write_close() by hand, or register it as a shutdown function: register_shutdown_function("session_write_close") . 您需要手动调用session_write_close()或将其注册为关闭函数: register_shutdown_function("session_write_close")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM