从PHP 5.6移至7.3后,为什么函数“ session_regenerate_id”停止工作?

时间:2020-05-21 04:30:19

标签: php pdo session-cookies

这是我在PHP 7.3上遇到的错误: 致命错误:未捕获的错误:在null上调用成员函数prepare() 这是我编写的示例代码。这段代码适用于5.6,但不适用于7.3:

include_once $_SERVER['DOCUMENT_ROOT'].'/files/sessions/class.sessions.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/files/sessions/class.database.php';

// Get instance to the system sessions object
$session = new Session();

session_regenerate_id();

这是我用来处理会话的课程:

<?php
class Session {
  private $db;

  public function __construct(){
    // Instantiate new Database object
    $this->db = new Database;

    // Set handler to overide $_SESSION
    session_set_save_handler(
      array($this, "_open"),
      array($this, "_close"),
      array($this, "_read"),
      array($this, "_write"),
      array($this, "_destroy"),
      array($this, "_gc")
    );

    // Session expiration
    ini_set('session.cookie_lifetime', 157680000);
    // Start the session
    session_start();
  }
  public function _open(){
    // If successful
    if($this->db){
      // Return True
      return true;
    }
    // Return False
    return false;
  }
  public function _close(){
    // Close the database connection
    // If successful
    if($this->db->close()){
      // Return True
      return true;
    }
    // Return False
    return false;
  }
  public function _read($id){
    // Set query
    $this->db->query('SELECT data FROM sessions WHERE id = :id');
    // Bind the Id
    $this->db->bind(':id', $id);
    // Attempt execution
    // If successful
    if($this->db->execute()){
      // Save returned row
      $row = $this->db->single();
      //Make sure it will not return NULL
      if (is_null($row['data'])) {
        return '';
      }
      // Return the data
      return $row['data'];
    }else{
      // Return an empty string
      return '';
    }
  }
  public function _write($id, $data){
    // Create time stamp
    $access = time();
    // Set query  
    $this->db->query('REPLACE INTO sessions VALUES (:id, :access, :data)');
    // Bind data
    $this->db->bind(':id', $id);
    $this->db->bind(':access', $access);  
    $this->db->bind(':data', $data);
    // Attempt Execution
    // If successful
    if($this->db->execute()){
      // Return True
      return true;
    }
    // Return False
    return false;
  }
  public function _destroy($id){
    // Set query
    $this->db->query('DELETE FROM sessions WHERE id = :id');
    // Bind data
    $this->db->bind(':id', $id);
    // Attempt execution
    // If successful
    if($this->db->execute()){
      // Return True
      return true;
    }
    // Return False
    return false;
  } 
  public function _gc($max){
    // Calculate what is to be deemed old
    $old = time() - $max;
    // Set query
    $this->db->query('DELETE FROM sessions WHERE access < :old');
    // Bind data
    $this->db->bind(':old', $old);
    // Attempt execution
    //if($this->db->execute()){
      // Return True
      //return true;
    //}
    // Return False
    return false;
  }
}
?>

如果有帮助,这是我们正在使用的数据库类:

<?php
//From:
//http://culttt.com/2013/02/04/how-to-save-php-sessions-to-a-database/

// Define database configuration
define("DB_HOST", $GLOBALS["db_servername"]);
define("DB_PORT", $GLOBALS["db_port"]);
define("DB_USER", $GLOBALS["db_username"]);
define("DB_PASS", $GLOBALS["db_password"]);
define("DB_NAME", $GLOBALS["db_dbname"]);

class Database{
    private $host      = DB_HOST;
    private $port      = DB_PORT;
    private $user      = DB_USER;
    private $pass      = DB_PASS;
    private $dbname    = DB_NAME;
    private $dbh;
    private $error;
    private $stmt;

    public function __construct(){
        // Set DSN
        $dsn = 'mysql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname;

        // Set options
        $options = array(
            PDO::ATTR_PERSISTENT    => true,
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
        );
        // Create a new PDO instanace
        try{
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        }
        // Catch any errors
        catch(PDOException $e){
            $this->error = $e->getMessage();
        }
    }

    public function query($query){
        $this->stmt = $this->dbh->prepare($query);
    }
    public function bind($param, $value, $type = null){
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }
    public function execute(){
        return $this->stmt->execute();
    }    

    public function resultset(){
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function single(){
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function rowCount(){
        return $this->stmt->rowCount();
    }

    public function lastInsertId(){
        return $this->dbh->lastInsertId();
    }

    public function beginTransaction(){
        return $this->dbh->beginTransaction();
    }

    public function endTransaction(){
        return $this->dbh->commit();
    }

    public function cancelTransaction(){
        return $this->dbh->rollBack();
    }

    public function debugDumpParams(){
        return $this->stmt->debugDumpParams();
    }

    public function close(){
      $this->dbh = null;
    }
}
?>

0 个答案:

没有答案