php sesson数据库存储
2022-03-24 PHP 1784
<?php /* * CREATE TABLE `prefix_session` ( `session_id` VARCHAR(255) NOT NULL, `session_expire` INT(11) UNSIGNED NOT NULL, `session_data` BLOB NULL, UNIQUE INDEX `session_id` (`session_id`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB; */ class DbSesson implements SessionHandlerInterface { private $link; public function open($savePath, $sessionName) { $link = mysqli_connect("127.0.0.1", "root", "root", "testdb"); if ($link) { $this->link = $link; return true; } else { return false; } } public function close() { mysqli_close($this->link); return true; } public function read($id) { $result = mysqli_query($this->link, "SELECT session_data FROM prefix_session WHERE session_id = '" . $id . "' AND session_expire > '" . date('Y-m-d H:i:s') . "'"); if ($row = mysqli_fetch_assoc($result)) { return $row['session_data']; } else { return ""; } } public function write($id, $data) { $DateTime = date('Y-m-d H:i:s'); $NewDateTime = date('Y-m-d H:i:s', strtotime($DateTime . ' + 1 hour')); $result = mysqli_query($this->link, "REPLACE INTO prefix_session SET session_id = '" . $id . "', session_expire = '" . $NewDateTime . "', session_data = '" . $data . "'"); if ($result) { return true; } else { return false; } } public function destroy($id) { $result = mysqli_query($this->link, "DELETE FROM prefix_session WHERE session_id ='" . $id . "'"); if ($result) { return true; } else { return false; } } public function gc($maxlifetime) { $result = mysqli_query($this->link, "DELETE FROM prefix_session WHERE ((UNIX_TIMESTAMP(session_expire) + " . $maxlifetime . ") < " . $maxlifetime . ")"); if ($result) { return true; } else { return false; } } } $handler = new DbSesson(); session_set_save_handler($handler, true); session_start(); $_SESSION['var1'] = "Hello db sesson!"; print_r($_SESSION);
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论