首頁 >後端開發 >php教程 >无法将php的session存入数据库

无法将php的session存入数据库

WBOY
WBOY原創
2016-06-06 20:37:451079瀏覽

<code><?php ob_end_clean();
if((int)ini_get('session.auto_start') > 0)
{
    ini_set('session.auto_start',0);
}
function zen_session_open()
{
    global $zen_session_db;
    $zen_session_db = new mysqli('localhost','root','wh36524','phptest');
    return TRUE;
}

function zen_session_close()
{
    global $zen_session_db;
    return $zen_session_db -> close();
}

function zen_session_read($sid)
{
    global $zen_session_db;
    $sql = 'select session_value from sessions where session_id="'.$sid.'"';
    $query = $zen_session_db->query($sql);
    if($zen_session_db -> errno)
    {
        die($zen_session_db->error);
    }
    if($query -> num_rows == 1)
    {
        $data = $query->fetch_assoc();
        return $data['session_value'];
    }
    else
    {
        return "";
    }
}

function zen_session_write($sid,$data)
{
    global $zen_session_db;
    $sql = 'select count(*) from sessions where session_id="'.$sid.'"';
    $query = $zen_session_db->query($sql);
    if($zen_session_db->errno)
    {
        die($zen_session_db->error);
    }
    if($query->num_rows > 0)
    {
        $sql = 'update session set session_value="'.$data.'",expire='.(time()+60*60).' where session_id="'.
               $sid.'"';
        $zen_session_db->query($sql);
    }
    else
    {
        $sql = 'insert into session values("'.$sid.'","'.(time()+60*60).'","'.$data.'")';
        $zen_session_db->query($sql);
    }
    /*
    if($zen_session_db->affected_rows > 0)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
    */
    return TRUE;
}

function zen_session_destroy($sid)
{
    global $zen_session_db;
    $sql = 'delete from session where session_id="'.$sid.'"';
    $zen_session_db->query($sql);
    $_SESSION = array();
    return TRUE;
}
function zen_session_gc($time)
{
    global $zen_session_db;
    $sql = 'delete from session where expire query($sql);
    return TRUE;
}

session_set_save_handler('zen_session_open','zen_session_close','zen_session_read','zen_session_write',
    'zen_session_destroy','zen_session_gc'
    );
session_name('zenID');
session_start();
$_SESSION['age'] = 16;
$_SESSION['color'] = 'green';
session_write_close();

</code>

不知道怎么回事,比到参考书写的,但是就是无法实现在数据库存储啊,我在另外一个页面拷贝了上面的代码,然后打印$_SESSION数组,但是是空的,不知道是哪里的问题?

<code><?php ob_end_clean();
if((int)ini_get('session.auto_start') > 0)
{
    ini_set('session.auto_start',1);
}

function zen_session_open()
{
    global $zen_session_db;
    $zen_session_db = new mysqli('localhost','root','wh36524','phptest');
    return TRUE;
}

function zen_session_close()
{
    global $zen_session_db;
    return $zen_session_db -> close();
}

function zen_session_read($sid)
{
    global $zen_session_db;
    $sql = 'select session_value from sessions where session_id="'.$sid.'"';
    $query = $zen_session_db->query($sql);
    if($zen_session_db -> errno)
    {
        die($zen_session_db->error);
    }
    if($query -> num_rows == 1)
    {
        $data = $query->fetch_assoc();
        return $data['session_value'];
    }
    else
    {
        return "";
    }
}

function zen_session_write($sid,$data)
{
    global $zen_session_db;
    $sql = 'select count(*) from sessions where session_id="'.$sid.'"';
    $query = $zen_session_db->query($sql);
    if($zen_session_db->errno)
    {
        die($zen_session_db->error);
    }
    if($query->num_rows > 0)
    {
        $sql = 'update session set session_value="'.$data.'",expire='.(time()+60*60).' where session_id="'.
               $sid.'"';
        $zen_session_db->query($sql);
    }
    else
    {
        $sql = 'insert into session values("'.$sid.'","'.(time()+60*60).'","'.$data.'")';
        $zen_session_db->query($sql);
    }
    /*
    if($zen_session_db->affected_rows > 0)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
    */
    return TRUE;
}

function zen_session_destroy($sid)
{
    global $zen_session_db;
    $sql = 'delete from session where session_id="'.$sid.'"';
    $zen_session_db->query($sql);
    $_SESSION = array();
    return TRUE;
}
function zen_session_gc($time)
{
    global $zen_session_db;
    $sql = 'delete from session where expire query($sql);
    return TRUE;
}

session_set_save_handler('zen_session_open','zen_session_close','zen_session_read','zen_session_write',
    'zen_session_destroy','zen_session_gc'
    );
session_name('zenID');
session_start();
//$_SESSION['age'] = 16;
//$_SESSION['color'] = 'green';
print_r($_SESSION);
session_write_close();


</code>

回复内容:

<code><?php ob_end_clean();
if((int)ini_get('session.auto_start') > 0)
{
    ini_set('session.auto_start',0);
}
function zen_session_open()
{
    global $zen_session_db;
    $zen_session_db = new mysqli('localhost','root','wh36524','phptest');
    return TRUE;
}

function zen_session_close()
{
    global $zen_session_db;
    return $zen_session_db -> close();
}

function zen_session_read($sid)
{
    global $zen_session_db;
    $sql = 'select session_value from sessions where session_id="'.$sid.'"';
    $query = $zen_session_db->query($sql);
    if($zen_session_db -> errno)
    {
        die($zen_session_db->error);
    }
    if($query -> num_rows == 1)
    {
        $data = $query->fetch_assoc();
        return $data['session_value'];
    }
    else
    {
        return "";
    }
}

function zen_session_write($sid,$data)
{
    global $zen_session_db;
    $sql = 'select count(*) from sessions where session_id="'.$sid.'"';
    $query = $zen_session_db->query($sql);
    if($zen_session_db->errno)
    {
        die($zen_session_db->error);
    }
    if($query->num_rows > 0)
    {
        $sql = 'update session set session_value="'.$data.'",expire='.(time()+60*60).' where session_id="'.
               $sid.'"';
        $zen_session_db->query($sql);
    }
    else
    {
        $sql = 'insert into session values("'.$sid.'","'.(time()+60*60).'","'.$data.'")';
        $zen_session_db->query($sql);
    }
    /*
    if($zen_session_db->affected_rows > 0)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
    */
    return TRUE;
}

function zen_session_destroy($sid)
{
    global $zen_session_db;
    $sql = 'delete from session where session_id="'.$sid.'"';
    $zen_session_db->query($sql);
    $_SESSION = array();
    return TRUE;
}
function zen_session_gc($time)
{
    global $zen_session_db;
    $sql = 'delete from session where expire query($sql);
    return TRUE;
}

session_set_save_handler('zen_session_open','zen_session_close','zen_session_read','zen_session_write',
    'zen_session_destroy','zen_session_gc'
    );
session_name('zenID');
session_start();
$_SESSION['age'] = 16;
$_SESSION['color'] = 'green';
session_write_close();

</code>

不知道怎么回事,比到参考书写的,但是就是无法实现在数据库存储啊,我在另外一个页面拷贝了上面的代码,然后打印$_SESSION数组,但是是空的,不知道是哪里的问题?

<code><?php ob_end_clean();
if((int)ini_get('session.auto_start') > 0)
{
    ini_set('session.auto_start',1);
}

function zen_session_open()
{
    global $zen_session_db;
    $zen_session_db = new mysqli('localhost','root','wh36524','phptest');
    return TRUE;
}

function zen_session_close()
{
    global $zen_session_db;
    return $zen_session_db -> close();
}

function zen_session_read($sid)
{
    global $zen_session_db;
    $sql = 'select session_value from sessions where session_id="'.$sid.'"';
    $query = $zen_session_db->query($sql);
    if($zen_session_db -> errno)
    {
        die($zen_session_db->error);
    }
    if($query -> num_rows == 1)
    {
        $data = $query->fetch_assoc();
        return $data['session_value'];
    }
    else
    {
        return "";
    }
}

function zen_session_write($sid,$data)
{
    global $zen_session_db;
    $sql = 'select count(*) from sessions where session_id="'.$sid.'"';
    $query = $zen_session_db->query($sql);
    if($zen_session_db->errno)
    {
        die($zen_session_db->error);
    }
    if($query->num_rows > 0)
    {
        $sql = 'update session set session_value="'.$data.'",expire='.(time()+60*60).' where session_id="'.
               $sid.'"';
        $zen_session_db->query($sql);
    }
    else
    {
        $sql = 'insert into session values("'.$sid.'","'.(time()+60*60).'","'.$data.'")';
        $zen_session_db->query($sql);
    }
    /*
    if($zen_session_db->affected_rows > 0)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
    */
    return TRUE;
}

function zen_session_destroy($sid)
{
    global $zen_session_db;
    $sql = 'delete from session where session_id="'.$sid.'"';
    $zen_session_db->query($sql);
    $_SESSION = array();
    return TRUE;
}
function zen_session_gc($time)
{
    global $zen_session_db;
    $sql = 'delete from session where expire query($sql);
    return TRUE;
}

session_set_save_handler('zen_session_open','zen_session_close','zen_session_read','zen_session_write',
    'zen_session_destroy','zen_session_gc'
    );
session_name('zenID');
session_start();
//$_SESSION['age'] = 16;
//$_SESSION['color'] = 'green';
print_r($_SESSION);
session_write_close();


</code>

提问应该尽可能多的提供线索,比如你的 SQL 语句是什么样的,这样别人在测试的时候方便多了,我现在还需要自己来创建:

<code>sql</code><code>CREATE TABLE `session` (
  `session_id` char(128) DEFAULT NULL,
  `expire` int(11) DEFAULT NULL,
  `session_value` text
) ENGINE=MyISAM DEFAULT CHARSET=utf8
</code>

环境信息也尽可能的提供,比如我的代码可以运行在 php 5.5.16-pl0-gentoo

问题1、你的表名有问题,读的时候用了,sessions ; 写的时候是用的 session

问题2、貌似通过 open 方法时打开的数据库链接在其它地方用不了,(这个原因我也不清楚,后面可以了解下为什么),去官方看了下,都是使用的 class 来完成的。所以把你的改成了 OOP 的形式测试可用。

<code><?php ob_end_clean();
if((int)ini_get('session.auto_start') > 0)
{
    ini_set('session.auto_start',0);
}

class MySqlSessionHandler implements SessionHandlerInterface
{
private $_db;

public function close()
{
    return $this->_db->close();
}

public function destroy($sessionn_id)
{
    $sql = 'delete from sessions where session_id="'.$session_id.'"';
    $this->_db->query($sql);
    $_SESSION = array();
    return TRUE;
}

public function gc($maxlifetime)
{
    $sql = 'delete from sessions where expire query($sql);
    return TRUE;
}

public function open($save_path, $name)
{
    $this->_db = new mysqli('localhost','root','password','test');
    return (boolean)$this->_db;
}

public function read($session_id)
{
    $sql = 'select session_value from sessions where session_id="'.$session_id.'"';
    $query = $this->_db->query($sql);
    if($this->_db->errno) {
        die($this->_db->error);
    }
    if($query -> num_rows == 1) {
        $session_data = $query->fetch_assoc();
        return $session_data['session_value'];
    } else {
        return "";
    }
}

public function write($session_id, $session_data)
{
    $sql = 'select count(*) as count from sessions where session_id="'.$session_id.'"';
    $query = $this->_db->query($sql);
    $result = $query->fetch_assoc();
    if($this->_db->errno) {
        die($this->_db->error);
    }
    if($result['count'] > 0)
    {
        $sql = 'update sessions set session_value=\''.$session_data.'\',expire='.(time()+60*60).' where session_id="'.
            $session_id.'"';
        $this->_db->query($sql);
    } else {
        $sql = 'insert into sessions values("'.$session_id.'","'.(time()+60*60).'",\''.$session_data.'\')';
        $this->_db->query($sql);
    }
    if($this->_db->affected_rows > 0) {
        return TRUE;
    } else {
        return FALSE;
    }
    }
}

$handler = new MySqlSessionHandler;
session_set_save_handler($handler, true);

session_name('zenID');
session_start();
$_SESSION['age'] = 17;
$_SESSION['color'] = 'green';
$_SESSION['color2'] = 'red';
</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn