当前位置:网站首页>PHP saves session data to MySQL database

PHP saves session data to MySQL database

2022-07-06 20:53:00 hello php

session_set_save_handler() Use cases of methods 

sql The statement is as follows :

CREATE TABLE `session` (
  `id` int(11) unsigned zerofill NOT NULL AUTO_INCREMENT,
  `data` varchar(255) DEFAULT NULL,
  `lapse_time` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

The code is as follows : 

<?php

/**
 *  Reference resources : http://c.biancheng.net/view/8192.html
 */
//  Connect to database 
function sess_open($savePath, $sessionName)
{
	echo "sess_open() Method <br/>";
	global $link;
	$link = mysqli_connect('localhost', 'root', 'root') or die(' Database connection failed !');
	mysqli_select_db($link, 'test') or die(' The database was not found !');
	return true;
}

//  Close database connection 
function sess_close()
{
	echo "sess_close() Method <br/>";
	global $link;
	mysqli_close($link);
	return true;
}

//  Read  Session  Content 
function sess_read($sessionId)
{
	echo "sess_read() Method <br/>";
	global $link;
	$time = time();
	$sql = "SELECT data FROM session WHERE id = '$sessionId' AND lapse_time > '$time'";
	$result = mysqli_query($link, $sql);
	$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	if ($row) {
//		return $row['data'];
		return serialize($row['data']);// pit !!! Need to serialize 
	} else {
		return serialize(false);// pit !!! Need to serialize 
	}
}


//  write in  Session
function sess_write($sessionId, $data)
{
	echo "sess_write() Method <br/>";
	global $link;
	$lapse_time = time() + (60 * 60);
	$sql = "SELECT data FROM session WHERE id = '$sessionId'";
	$result = mysqli_query($link, $sql);
	if (mysqli_num_rows($result) == 0) {
		$ins_sql = "INSERT INTO session VALUES('$sessionId', '$data', '$lapse_time')";
		$res = mysqli_query($link, $ins_sql);
	} else {
		$up_sql = "UPDATE session SET data = '$data', lapse_time = '$lapse_time' WHERE id = '$sessionId'";
		$res = mysqli_query($link, $up_sql);
	}
	return $res;
}

//  Delete  Session
function sess_destroy($sessionId)
{
	echo "sess_destroy() Method <br/>";
	global $link;
	$sql = "DELETE FROM session WHERE id = '$sessionId'";
	return mysqli_query($link, $sql);
}

//  Clean up invalid  Session
function sess_gc()
{
	echo "sess_gc() Method <br/>";
	global $link;
	$time = time();
	$sql = "DELETE FROM session WHERE lapse_time < '$time'";
	$res = mysqli_query($link, $sql);
	return $res;
}


//session_set_save_handler('sess_open', 'sess_close', 'sess_read', 'sess_write', 'sess_destroy', 'sess_gc');
//echo ini_get("session.save_handler") . PHP_EOL; //  return  user
//die;

//**********************************************************************
//ini_set("session.save_handler", "user");//user For custom 
session_set_save_handler('sess_open', 'sess_close', 'sess_read', 'sess_write', 'sess_destroy', 'sess_gc');
session_start();
$_SESSION['user'] = 'admin';

var_dump($_SESSION['user']);

The result of the browser visit is as follows :

  The database data is as follows :

 

原网站

版权声明
本文为[hello php]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131142170415.html