Classes about custom SESSION handlers

This class is a program that uses the database as the persistence of the SESSION data area

So first go to the data table structure: session (table name)


#Create database test
create database if not exists test charset=utf8;
#Open the database
use test;
#create table
create table if not exists `session` (
	session_id varchar(40) not null default '',
	session_content text,
	last_time int not null default 0,
	primary key(session_id)
)charset=utf8 engine=myisam;

#Add user to database test: test2018, password: 1234abcd;
grant all on test.* to 'test2018

Then there should be a scheme for using PDO to operate the database, the PDO class, in another article

https://blog.csdn.net/ddv1999/article/details/80167064


Relevant code for this example


<?php
require_once 'pdodb.class.php';
class model extends pdodb{
	//protected $_dao;
	// private $ _dao;
	public $_dao;
	function  __construct(){
		$cs=array(//connect the array
			'host'       =>'127.0.0.1',
			'port'       =>'3306',
			'username'   =>'test2018',
			'password'   =>'1234abcd',
			'dbname'     =>'test',
			'charset'    =>'utf8',
		);
		$this->_dao=pdodb::getdb($cs);
		//$this->_dao->test();
		//echo "<pre>";
		//var_dump($this->_dao);
		//echo "</pre>";
	}
}
class sessiondb extends model{
		public function __construct() {
		//Enable the construction of the parent class. For all inherited classes, when constructing, pay attention to overwriting the construction of the parent class.
		//This class is pitted by the construction of the parent class. Without this sentence, the _dao of the parent class is empty.
		parent::__construct();
		//Set the session handler
		ini_set('session.save_handler', 'user');
		session_set_save_handler(
			array($this,'uBegin'),
			array($this,'uEnd'),
			array($this,'uRead'),
			array($this,'uWrite'),
			array($this,'uDelete'),
			array($this,'uGC')
		);
		// turn on
		session_start();
		echo "<pre>";
		//var_dump($this->_dao);//The construction of the parent class is not called, here is NULL
		//var_dump(new self());
		echo "</pre>";
	}
	// private $ _dao;
	//1-Begin start
	function uBegin()
	{
		echo "<br>begin";
		return true;
	}
	//2-End end
	function uEnd()
	{
		echo "<br>end";
		return true;
	}
	//3-Read read
		/**
	 * read operation
	 * Execution timing: Executed during session mechanism startup
	 * Work: Read content from the current session data area
	 * @param $sess_id string
	 * @return string
	 */
	function uRead($sess_id)
	{
		echo "<br>read";
		echo "<br>本次ID:$sess_id";
		$sql = "SELECT session_content FROM session WHERE session_id='$sess_id'";
		//echo "<pre>";
		//var_dump($this->_dao);
		//echo "</pre>";
		$a=$this->_dao->getone($sql);
		echo "<pre>";
		var_dump($a);
		//var_dump($a);
		echo "</pre>";
		echo "<br>".gettype($a);
		//the();
		if(gettype($a)=='string'){
			echo "<br>Find and return SESSION data";
			return $a;
		}
		echo "<br>No SESSION data exists, return empty string ''";

		return '';

	}
	//4-Write写
		/**
	 * write operation
	 * Execution timing: At the end of the script cycle, PHP is finishing finishing
	 * Job: Persist the session data processed by the current script to the database!
	 * @param $sess_id string
	 * @param $sess_content string Serialized session content string
	 * @return bool
	 */
	uWrite function ($ sess_id, $ sess_content)
	{
		echo "<br>write";
		echo "<br>The ID written this time:".$sess_id."The content written this time".$sess_content;
		$sql = "REPLACE INTO `session` VALUES ('$sess_id', '$sess_content', unix_timestamp())";
		$a=$this->_dao->getone($sql);
		//echo "<pre>";
		//var_dump($a);
		//echo "</pre>";
		//the();
		if($a===false){
			echo "<br>Failed to execute";
			return false;
		}
		echo "<br>Successful execution";
		return true;

	}
	//5-Delete delete
		/**
	 * delete operation
	 * Execution timing: Called when session_destroy() is called to destroy the session
	 * Job: Delete the data area (record) of the current session
	 * @param $sess_id string
	 * @return bool
	 */
	function uDelete($sess_id)
	{
		echo "<br>delete";
		$sql = "DELETE FROM `session` WHERE session_id='$sess_id'";
		$a=$this->_dao->getone($sql);
		echo "<pre>";
		var_dump($a);
		echo "</pre>";
		if($a===false){
			echo "<br>Failed to execute";
			return false;
		}
		echo "<br>Successful execution";
		return true;
	}
	//6-GC recovery mechanism
		/**
	 * Garbage collection operations
	 * Execution timing: When the session mechanism is turned on, there is a probability of execution
	 * Job: delete all expired session data areas
	 * @param $max_lifetime
	 * @return bool
	 */
	function uGC()
	{
		echo "<br>GC";
		//delete
		//$a=get_cfg_var('session.gc_maxlifetime');
		$a=5;//In order to see the execution effect of GC faster, the validity period of the data area is changed to 5 seconds
		$sql = "DELETE FROM `session` WHERE last_time<unix_timestamp()-$a";
		$a=$this->_dao->getone($sql);
		echo "<pre>";
		var_dump($a);
		echo "</pre>";
		if($a===false){
			echo "<br>Failed to execute";
			return false;
		}
		echo "<br>Successful execution";
		return true;

		//return $this->_dao->getone($sql);
		//return true;
	}
}

//$aaa=new model();

//1 open session
new sessiondb();  

$_SESSION['admin']="Login successful";

//echo "<pre>";
// var_dump ($ _ SESSION);
//echo "</pre>";



/*
//Configure PHP.INI command to improve the probability of recycling
ini_set('session.gc_probability','1');
ini_set('session.gc_divisor','1');

//1 open session
//session_start();
new sessiondb;  
//2, clear session information
//die("I can't think of any other place to destroy the $_SESSION array");
$_SESSION = array();  
//3, clear the client sessionid
if(isset($_COOKIE[session_name()])){  
	setCookie(session_name(),'',time()-1,'/');
}
//4, completely destroy the session
session_destroy();
*/

 


It is just an example for learning, the specific application should be modified according to the actual situation, mainly to understand the operating mechanism

For example: if the read method is used, the return value (empty string/string) will affect the call of the GU recycling mechanism, which is a big pit



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325445446&siteId=291194637