简体   繁体   English

PHP会话save_handler用户(mysql)无法保存

[英]PHP session save_handler user (mysql) won't save

PHP sessions work by default with my configuration, if I just go session_start() and try the standard session increment test it works. 如果我只是去session_start()并尝试标准的会话增量测试,则PHP会话默认在我的配置下工作。

if (!isset($_SESSION['count'])) {
    $_SESSION['count'] = 0;
} else {
    $_SESSION['count']++;
}

However I want to use a MySQL table for session storage. 但是我想使用MySQL表进行会话存储。 I've put together my sessions.php file with all the functions, copied them right out of a book like a n00b, and the functions work (affect the database) if I call them like regular functions, but using the standard test above does not work. 我将我的sessions.php文件与所有函数放在一起,将它们像n00b一样直接从书中复制出来,如果我像常规函数那样调用它们,这些函数就可以工作(影响数据库),但是使用上面的标准测试可以不行。 It sets the session for just the page load, and no change in the database. 它只为页面加载设置会话,而对数据库没有任何更改。 I put a line in each function to log each call, and the log reflects that the functions are being called by session_start() . 我在每个函数中放置一行以记录每个调用,并且该日志反映出session_start()正在调用这些函数。

Here's what my code looks like: 这是我的代码:

session_module_name("user");
session_set_save_handler("session_open", "session_close", 
"session_read", "session_write", "session_remove", "session_gc");
session_start();

session_open, etc, being the name of my functions. session_open等,是我函数的名称。 I've even tried another set of functions out of an o'rly example, and got the same results. 我什至从一个糟糕的例子中尝试了另一组函数,并得到了相同的结果。

Any ideas why? 有什么想法吗? session_register() also yields the same results. session_register()也产生相同的结果。

EDIT: here are the actual functions, I apologize for the length, but I log everything in dev. 编辑:这是实际的功能,对于长度,我深表歉意,但是我将所有记录在dev中。

function session_db(){
    return("my_db_name");
    }
function session_table(){
    return("sessions_table");
    }
function session_log($message){
    if($file = fopen($application["siteroot"] . 'log/session.txt', "a")){
        fwrite($file, date("Y-m-d H:i:s ") . $message . "\n");
        fclose($file);
        }
    }
function session_open($path, $name){
    session_log("session_open");
    return(true);
    }
function session_close(){
    session_log("session_close");
    return(true);
    }
function session_read($id){
    session_log("session_read");
    if(!mysql_select_db(session_db())){
        session_log("session_read select database error: " . mysql_error());
        return(false);
        }
    $sql = "select * from " . session_table() . " where id='" . $id . "'";
    if(!$result = mysql_query($sql)){
        session_log("MySQL error: " . mysql_error() . " with SQL: " . $sql);
        return(false);
        }
    if(mysql_num_rows($result)){
        session_log("MySQL query returned " . mysql_num_rows($result) . "rows.");
        $row = mysql_fetch_assoc($result);
        session_log("session_read returned " . $row["data"]);
        return($row["data"]);
        }
    else{
        session_log("session_read found zero rows with SQL: " . $sql);
        return("");
        }
    }
function session_write($id, $data){
    session_log("session_write");
    if(!mysql_select_db(session_db())){
        session_log("session_write select database error: " . mysql_error());
        return(false);
        }
    $sql = "update " . session_table() . " set data = '" . addslashes($data) . "', time=null";
    if(isset($PHP_AUTH_USER)){
        $sql .= ", user='" . addslashes($PHP_AUTH_USER) . "'";
        }
    $sql .= " where id='" . $id . "'";
    if(!$result = mysql_query($sql)){
        session_log("session_write error " . mysql_error() . " with SQL: " . $sql);
        return(false);
        }
    if(mysql_affected_rows()){
        session_log("session_write update affected " . mysql_affected_rows() . " rows with SQL: " . $sql);
        return(true);
        }
    session_log("session_write updated zero rows with SQL: " .$sql);
    $sql = "insert into " . session_table() . "(data,id) values('" . addslashes($data) . "','" . $id . "')";
    if(!$result = mysql_query($sql)){
        session_log("session_write error " . mysql_error() . "with SQL: " . $sql);
        return(false);
        }
    else{
        session_log("mysql_write inserted with SQL: " . $sql);
        return(true);
        }
    }
function session_remove($id){
    session_log("session_remove");
    if(!mysql_select_db(session_db())){
        session_log("session_remove select database error: " . mysql_error());
        return(false);
        }
    $sql = "delete " . session_table() . " where id='" . $id . "'";
    if($result = mysql_query($sql)){
        session_log("MySQL query delete worked");
        return(true);
        }
    else{
        session_log("MySQL update error: " . mysql_error() . " with SQL: " . $sql);
        return(false);
        }
    }
function session_gc($life){
    session_log("session_gc");
    if(!mysql_select_db(session_db())){
        session_log("session_gc select database error: " . mysql_error());
        return(false);
        }
    $sql = "delete " . session_table() . " where time < '" . date("YmdHis", time() - $life) . "'";
    print("session_gc sql: " . $sql);
    if($result = mysql_query($sql)){
        session_log("session_gc deleted " . mysql_affected_rows() . " rows.");
        return(true);
        }
    else{
        session_log("session_gc error: " . mysql_error() . " with SQL: " . $sql);
        return(false);
        }
    }

我认为您不需要调用session_module_name,尝试将其注释掉,看看会发生什么。

There are a couple of things... 有几件事...

  1. We might need to see, at the very least, the actual functions. 我们可能至少需要查看实际功能。

  2. You probably want to register a shutdown function, your writes are probably being called too late to save to the database. 您可能想注册一个关闭函数,您的写入可能被调用太晚而无法保存到数据库。

    register_shutdown_function('session_write_close'); register_shutdown_function( 'session_write_close');

Just to clarify, the reason for the above is that the write and close functions are normally called after objects are destroyed. 只是为了澄清,上面的原因是在销毁对象后通常会调用write和close函数。 This call will ensure that these are made before object destruction. 该调用将确保在销毁对象之前进行这些操作。

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

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