Get the App
SLTechnology News&Howtos  ›  Development  › 

How PHP implements classes that store session information in a database

Shulou Source: shulou.com Published: 2022-06-03 19:47:17 10月06日 Update

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!

Tags: Data database information storage interface time article longest fascinating code function garbage done manual more knowledge structure industry information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MariaDB Xiaomi NVidia Shulou Technology