


Optimize the PHP code that uses mysql to store sessions_PHP tutorial
I have written two articles before, "Customized SESSION (2) - Database Saving" and "Why I Don't Use Session"
But later I found problems with both. The former processing is almost useless in practice, and session recycling has to be handled separately. The latter frequently operates the database, causing big performance problems.
After careful consideration in the past two days, I have roughly given a plan, but there is no specific and detailed test.
1. Combine session processing and statistics. Visitors are also recorded.
2. Completely use database and cookies to simulate session functions.
3. The user’s operations on the session should be completed in one SQL statement as much as possible. When the session is not needed, there is absolutely no more than one query.
4. For the sake of efficiency, session recycling is not integrated, but an interface is provided and can be called to implement it.
The code is given temporarily without detailed explanation.
sql
CREATE TABLE `*****_session` (
`sid` char(32) NOT NULL,
`uid` int(10) NOT NULL,
`username` char(32) NOT NULL,
`usertype` tinyint(1) NOT NULL,
`activetime` int(10) NOT NULL,
`expiry` int( 10) NOT NULL,
`ip` char(15) NOT NULL,
`url` char(80) NOT NULL,
`value` char(255) NOT NULL,
PRIMARY KEY ( `sid`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8;
php code
class session{
private $_sessionPrex= '';//session prefix
private $_time = '';//current time
private $_model = null;//database operation model
private $_expiry = 1200;//session validity time
private $_domain = '';//session scope
protected $isNew = 0;// Determine operation action 0 Update 1 Added
protected $session = array();//Corresponding session record
public function __construct($options){
$this-> _setOptions($options);
If(empty($this->_time))$this->_time = time();
$this->session['activetime'] = $this- >_time; value){
if(in_array($key,array('uid','username','usertype','url','expiry'))){
if($key == 'expiry' ){
$this->_setCookie($this->_sessionPrex.'_sid',$this->session['sid'],$value);
$this->_setCookie($ this->_sessionPrex.'_uid',$this->session['uid'],$value);
}else{
$other = $this->session['value'];
$other[$key] = $value;
$this->session['value'] = $ other;
} }
}
public function get($key){
',' Expiry '))))) {
Return $ this-& gt; session [$ key];
} else {
if (ISSET ($ this- & gt; session [' value '] [$ key])){
return $this->session['value'][$key];
return null;
} }
public function gc($file,$time = 1200){
$lasttime = file_get_contents($file);
if($lasttime + $time<$this->_time){
file_put_contents($file,$this->_time);
return $this->_model->delete('activetime+expiry<'.$this->_time);
}
}
public function destroy(){
$this->session['uid'] = 0;
$this->session['username'] = '';
$this->session['usertype'] = -1;
$this->session['expiry'] = $this->_expiry;
$this->session['value'] = array();
$this->_setCookie($this->_sessionPrex.'_sid',$this->session['sid'],$this->_expiry);
$this->_setCookie($this->_sessionPrex.'_uid',$this->session['uid'],$this->_expiry);
}
public function __destruct(){
$this->_save();
}
private function _save(){
$dbSession = $this->session;
$dbSession['value'] = serialize($dbSession['value']);
if(strlen($dbSession['value'])>255)$this->_error('session->value is too long!');
if($this->isNew == 1){
//增加
$this->_model->insert($dbSession);
}else{
//更新
$sid = $dbSession['sid'];
$this->_model->update(array_slice($dbSession,1),'sid=''.$sid.''');
}
}
private function _getSession($sid){
$dbSession = $this->_model->detail('sid = ''.$sid.''');
if(!$dbSession)return false;
$dbSession['value'] = unserialize($dbSession['value']);
$this->session = array_merge($dbSession,$this->session);
return true;
}
private function _getSid(){
$sid = strip_tags($_COOKIE[$this->_sessionPrex.'_sid']);
if(strlen($sid)==32){
if($this->_getSession($sid)){
return true;
}
}else{
$sid = md5(time().mt_rand(1000,10000));
$this->_setCookie($this->_sessionPrex.'_sid',$sid);
}
$this->_setCookie($this->_sessionPrex.'_uid',0);
$this->session = array(
'uid' => 0,
'username' => '',
'usertype' => -1,
'activetime' => $this->_time,
'ip' => $this->_getip(),
'url' => strip_tags($_SERVER['REQUEST_URI']),
'expiry' =>$this->_expiry,
'value' => array()
);
$this->isNew = 1;
$this->session['sid'] = $sid;
}
private function _setCookie($name,$value,$expiry=0){
if(empty($expiry))$expiry = $this->_expiry;
if(empty($this->_domain)){
setcookie($name,$value,$this->_time + $expiry,'/');
}else{
setcookie($name,$value,$this->_time + $expiry,'/',$this->_domain);
}
}
private function _getip(){
return getip();
}
private function _setOptions($options){
foreach ($options as $key=>$value){
if(in_array($key,array('sessionPrex','time','model','expiry','domain'))){
$key = '_'.$key;
$this->$key = $value;
}
}
}
private function _error($msg){
throw new Phpbean_Exception($msg);
}
}
?>
(注意,该代码不能直接使用,本文主要是提供一种思路)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

Arrays are linear data structures used to process data in programming. Sometimes when we are processing arrays we need to add new elements to the existing array. In this article, we will discuss several ways to add elements to the end of an array in PHP, with code examples, output, and time and space complexity analysis for each method. Here are the different ways to add elements to an array: Use square brackets [] In PHP, the way to add elements to the end of an array is to use square brackets []. This syntax only works in cases where we want to add only a single element. The following is the syntax: $array[] = value; Example
