Unable to save PHP session to Database

Source: Internet
Author: User
Tags php session

   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 &Gt 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 (); 

Do not know how, than to the reference book, but it is impossible to achieve in the database storage Ah, I copied the above code on another page, and then print the $_session array, but is empty, do not know where is the problem?


   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 &Gt 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 ';p rint_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 &Gt 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 (); 

Do not know how, than to the reference book, but it is impossible to achieve in the database storage Ah, I copied the above code on another page, and then print the $_session array, but is empty, do not know where is the problem?


   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 &Gt 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 ';p rint_r ($_session); Session_write_close ();

Asking questions should provide as many clues as possible, such as what your statement looks like SQL , so that others are more convenient when testing, and now I 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

Environment information is provided as much as possible, such as my code can be run in PHP 5.5.16-pl0-gentoo

Question 1: You have a problem with the name of your table, you use it when you read it,sessions it, you write it with the session .

Question 2, seemingly through the open method of opening the database link in other places can not be used, (this reason I do not know, the latter to understand why), to the official look under, are used by the class to complete. So turn your back into the form of OOP test available.


   0) {ini_set (' Session.auto_start ', 0);} Class Mysqlsessionhandler implements Sessionhandlerinterface{private $_db;public function close () {return $this->_d B->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.