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();
不知道怎麼回事,比到參考書寫的,但是就是無法實現在資料庫儲存啊,我在另外一個頁面拷貝了上面的代碼,然後列印$_SESSION數組,但是是空的,不知道是哪裡的問題?
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();
回複內容:
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();
不知道怎麼回事,比到參考書寫的,但是就是無法實現在資料庫儲存啊,我在另外一個頁面拷貝了上面的代碼,然後列印$_SESSION數組,但是是空的,不知道是哪裡的問題?
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();
提問應該儘可能多的提供線索,比如你的 SQL
語句是什麼樣的,這樣別人在測試的時候方便多了,我現在還需要自己來建立:
sql
CREATE TABLE `session` ( `session_id` char(128) DEFAULT NULL, `expire` int(11) DEFAULT NULL, `session_value` text) ENGINE=MyISAM DEFAULT CHARSET=utf8
環境資訊也儘可能的提供,比如My Code可以運行在 php 5.5.16-pl0-gentoo
問題1、你的表名有問題,讀的時候用了,sessions ; 寫的時候是用的 session
問題2、貌似通過 open 方法時開啟的資料庫連結在其它地方用不了,(這個原因我也不清楚,後面可以瞭解下為什麼),去官方看了下,都是使用的 class 來完成的。所以把你的改成了 OOP 的形式測試可用。
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';