如何将会话存储在数据库中,然后在登录后调用它,并在php中注销时销毁它


How can store Sessions in Database then call it after log in and destroy when log off in php

我的代码将会话存储在数据库中,没有问题,但我不知道如何从数据库中调用它,以及如何在用户注销时销毁它

我的页面工作方式如下:

例如,要登录,用户将转到域----xxx.xxxx.com如果登录正确,它会将它们重定向到另一个域----yyyy.com

我不知道如何在登录后从域yyyy.com调用会话

我存储会话的代码:

文件名----MySQLDatabase.class.php

<?php
include("Config.inc.php");
class MySQLDatabase{
private $db;
private $hostname;
private $username;
private $password;
private $schema;
function __construct() {
    if(func_num_args() == 0){
        $this->hostname = conf_hostname;
        $this->username = conf_username;
        $this->password = conf_password;
        $this->schema = conf_schema;
    }
    else{
        $params = func_get_args();
        $this->hostname = $params[0];
        $this->username = $params[1];
        $this->password = $params[2];
        $this->schema = $params[3];
    }
    }
    private function open(){
    $this->db = mysql_connect($this->hostname, $this->username, $this-              >password) or die ('Error connecting to mysql');
    mysql_select_db($this->schema, $this->db);
   }
  public function executeQuery($query){
    $this->open();
      $results = mysql_query($query, $this->db) or die ("Error in query:      $query. ".mysql_error());
    return $results;
  }
  public function close(){
    mysql_close($this->db);
  }
 }
 ?>

文件名----DatabaseSessionHandler.class.php

    <?php
include_once ("MySQLDatabase.class.php");
class DatabaseSessionHandler {
private $db;
public function _open($save_path, $session_name) {
    $this -> db = new MySQLDatabase();
    return true;
}
public function _close() {
    $this -> db -> close();
}
function _read($id) {
    $id = mysql_real_escape_string($id);
    $query = "SELECT data
            FROM SESSION
            WHERE id = '$id'";
    if ($result = $this -> db -> executeQuery($query)) {
        if (mysql_num_rows($result)) {
            $record = mysql_fetch_assoc($result);
            return $record['data'];
        }
    }
    return '';
}
function _write($id, $data) {
    $access = time();
    $id = mysql_real_escape_string($id);
    $access = mysql_real_escape_string($access);
    $data = mysql_real_escape_string($data);
    $query = "REPLACE
            INTO SESSION
            VALUES ('$id', '$access', '$data')";
    return $this -> db -> executeQuery($query);
}
function _destroy($id) {
    $id = mysql_real_escape_string($id);
    $query = "DELETE
            FROM SESSION
            WHERE id = '$id'";
    return $this -> db -> executeQuery($query);
}
function _clean($max) {
    $old = time() - $max;
    $old = mysql_real_escape_string($old);
    $query = "DELETE
            FROM SESSION
            WHERE access < '$old'";
    return $this -> db -> executeQuery($query);
}
public function killUserSession($username){
    $query = "delete from SESSION where data like('%userID|s:%'"".       mysql_real_escape_string($username) ."%'";first_name|s:%')";
    $this->db->executeQuery($query);
}
}
?>

上面的代码工作起来没有问题,但我不知道如何调用存储在数据库中的会话。。。

我不知道如何调用存储在数据库中的会话。。。

您最好的资源是详细阅读本页:http://php.net/manual/en/class.sessionhandlerinterface.php。您应该更改DatabaseSessionHandler类,使其实现SessionHandlerInterface,然后调用session_set_save_handler()。示例在页面上。

设置好后,数据库中的会话将自动创建并销毁。

例如,要登录,用户会转到域----xxx.xxxx.com。如果登录正确,则会将他们重定向到另一个域----yyyy.com

我不知道如何在登录后从域yyyy.com调用会话

在正常的用例中,您不能真正做到这一点,因为会话绑定到cookie,而cookie不会在站点之间传播。因此,您将不得不将会话ID存储在URL中。有关如何做到这一点的更多信息,请阅读本页:http://php.net/manual/en/session.idpassing.php

相关文章: