In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how PHP realizes the class of storing session information in the database. I hope you will get something after reading this article. Let's discuss it together.
The specific analysis is as follows:
SessionHandlerInterface interface is the built-in interface of PHP, so you can implement it directly.
Specifically, you can see the explanation of session_set_save_handler function in php manual!
The PHP code is as follows:
/ * the class * table structure in which session information is stored in the database: * CREATE TABLE IF NOT EXISTS `sessioninfo` (* `sid` varchar) NOT NULL,* `value`text NOT NULL,* `accounation` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,* PRIMARY KEY (`sid`) *) ENGINE=InnoDB DEFAULT CHARSET=utf8;*/class MySessionHandler implements SessionHandlerInterface {/ * * @ access private * @ var object database connection * / private $_ dbLink / * * @ access private * @ var string saves the table name of session * / Private $_ sessionTable; / * * @ access private * @ var string session name * / private $_ sessionName; / * @ const Expiration time * / const SESSION_EXPIRE = 10 Public function _ _ construct ($dbLink, $sessionTable) {if (! is_object ($dbLink)) {return false;} $this- > _ dbLink = $dbLink; $this- > _ sessionTable = $sessionTable } / * Open * @ access public * @ param string $session_save_path path to save session * @ param string $session_name session name * @ return integer * / public function open ($session_save_path, $session_name) {$this- > _ sessionName = $session_name; return 0 } / * close * @ access public * @ return integer * / public function close () {return 0 } / * close session * @ access public * @ param string $session_id session ID * @ return string * / public function read ($session_id) {$query = "SELECT value FROM {$this- > _ sessionTable} WHERE sid = {$session_id} AND UNIX_TIMESTAMP (expiration) +". Self::SESSION_EXPIRE. "> UNIX_TIMESTAMP (NOW ())"; $result = $this- > _ dbLink- > query ($query); if (! isset ($value) | | empty ($value)) {$value = "; return $value;} $this- > _ dbLink- > query (" UPDATE {$this- > _ sessionTable} SET expiration = CURRENT_TIMESTAMP () WHERE sid = {$session_id} "); $value = $result- > fetch_array () $result- > free (); return $value ['value'] Write session * @ access public * @ param string $session_id session ID * @ param string $session_data session data * @ return integer * / public function write ($session_id, $session_data) {$query = "SELECT value FROM {$this- > _ sessionTable} WHERE sid ='{$session_id} 'AND UNIX_TIMESTAMP (expiration) +". Self::SESSION_EXPIRE. "> UNIX_TIMESTAMP (NOW ())"; $result = $this- > _ dbLink- > query ($query); $result = $result- > fetch_array (); if (! empty ($result)) {$result = $this- > _ dbLink- > query ("UPDATE {$this- > _ sessionTable} SET value = {$session_data} WHERE sid = {$session_id}") } else {$result = $this- > _ dbLink- > query ("INSERT INTO {$this- > _ sessionTable} (sid, value) VALUES ('{$session_id}','{$session_data}')");} if ($result) {return 0;} else {return 1 }} / * fascinating session * @ access public * @ param string $session_id session ID * @ return integer * / public function destroy ($session_id) {$result = $this- > _ dbLink- > query ("DELETE FROM {$this- > _ sessionTable} WHERE sid ='{$session_id}'"); if ($result) {return 0 } else {return 1;}} / * * garbage collection * @ access public * @ param string $maxlifetime session maximum lifetime * @ return integer * / public function gc ($maxlifetime) {$result = $this- > _ dbLink- > query ("DELETE FROM {$this- > _ sessionTable} WHERE UNIX_TIMESTAMP (expiration) < UNIX_TIMESTAMP (NOW ()) -". Self::SESSION_EXPIRE); if ($result) {return 0;} else {return 1;}} $dbLink = new mysqli ("localhost", "root", "root", "test"); $sessionTable = "sessioninfo"; $handler = new MySessionHandler ($dbLink, $sessionTable); session_set_save_handler ($handler); session_start (); $_ SESSION ['name'] = "test" Echo $_ SESSION ["name"]; / / session_destroy (); after reading this article, I believe you have a certain understanding of "how PHP implements the class of storing session information in the database". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.