Php sessions cannot be stored in the database.

Source: Internet
Author: User
{Code ...} I don't know what's going on. I wrote it in reference books, but I just couldn't store it in the database. I copied the above Code on another page and printed the $ _ SESSION array, but it is empty. I don't know where the problem is? {Code ...}

   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 < '.time();    $zen_session_db->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();

I don't know what's going on. I wrote it in reference books, but I just couldn't store it in the database. I copied the above Code on another page and printed the $ _ SESSION array, but it is empty. I don't know where the problem is?


   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 < '.time();    $zen_session_db->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();

Reply content:

   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 < '.time();    $zen_session_db->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();

I don't know what's going on. I wrote it in reference books, but I just couldn't store it in the database. I copied the above Code on another page and printed the $ _ SESSION array, but it is empty. I don't know where the problem is?


   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 < '.time();    $zen_session_db->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();

Ask as many clues as possible, such as yourSQLWhat is the statement, so that it is more convenient for others to test, and I still need to create it myself:

sqlCREATE TABLE `session` (  `session_id` char(128) DEFAULT NULL,  `expire` int(11) DEFAULT NULL,  `session_value` text) ENGINE=MyISAM DEFAULT CHARSET=utf8

The environment information is provided as much as possible. For example, my code can run inPhp 5.5.16-pl0-gentoo

Question 1: Your table name is incorrect. It is used for reading,Sessions; Used for writingSession

Question 2: It seems that the database link opened in the open method cannot be used in other places (I am not sure about this reason, but I can understand why later). I went to the official site and checked it out, all are completed using the class. So I changed your form to OOP to test availability.


   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 < '.time();    $zen_session_db->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';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.