Table of Contents
php strictly controls the session expiration time
Home Backend Development PHP Tutorial PHP strictly controls session expiration time_PHP tutorial

PHP strictly controls session expiration time_PHP tutorial

Jul 12, 2016 am 09:04 AM
time

php strictly controls the session expiration time

After working for a while, I believe everyone must have encountered a problem:
1. The front-end user automatically disconnected without knowing why.
2. After logging out from the background, all front-end users will also be offline.
3. I want to control my users to automatically go offline after half an hour, but I found that even changing the configuration file doesn’t work.
All the above problems were encountered by me recently. Later, I found out through inquiries: PHP’s session mechanism is controlled by several parameters at the same time. I won’t write down the specific ones. They are a probability and a maximum expiration time. , there is also a session storage path. In php.ini we can see that the default expiration time of PHP session is 24 minutes, which means that if we do not operate the page for 24 minutes, the session will expire. Of course, this is Ideally, after 24 minutes, PHP will initiate a session recycling mechanism. This mechanism is used to detect whether the change time of the session file in the default storage directory is 24 minutes ago. If so, delete the session. Of course, this is also an ideal state. This is the probability mentioned earlier. The session recycling mechanism is triggered based on probability. That is to say, even if your session is a file 24 minutes ago, if the recycling mechanism is triggered, your session will still not expire. This is of course also This is not what we want. In order to solve this problem, the third parameter I mentioned earlier appears, which is the storage path of the session. If you do not enable session.save_path inside php.ini, then the session will not have a file. Generated, so in order to control the session more effectively, we open it and fill in a path, or use the session_save_path("...") function in the file to define the storage path of this session. Another important point is , that is, if the session is stored in a path defined by ourselves, the seesion recycling mechanism will not work. So we can only control the expiration time of the session ourselves.
The following is an expiration processing class about session that I wrote based on my understanding
<?php 
class Session{
private $savePath;//存储session的路径,必须是绝对路径
private $time;//存储session的过期时间,单位是秒
private $sessionName;//session的名字
private $sessionValue;//session的值

public function __construct($savePath)
{
//将session存入指定的目录
$this->savePath = $savePath;
            //注意:这个一定要写在session_start前面
session_save_path($this->savePath); 

session_start();//开启session
if(!is_dir($this->savePath))
{
                //默认为最大的权限 0777
mkdir($this->savePath) or die(&#39;系统错误!&#39;);
}
}

//创建session  一共三个参数,
               // $name->session名字 
               // $val->session值 
               // $time->过期时间,默认为30分钟
public function setSession($name,$val,$time=1800)
{
$this->sessionName  = $name;
$this->sessionValue  = $val;
$this->time       = $time;
if(!isset($_SESSION[$this->sessionName]))
{
if(is_array($this->sessionValue))
{
foreach($this->sessionValue as $key=>$val)
{
$_SESSION[$this->sessionName][$key] = $val;
}
}
else
{
$_SESSION[$this->sessionName][&#39;val&#39;]   = $this->sessionValue;
}
$_SESSION[$this->sessionName]["startTime"] = time();
}
//这时候说明session已经存在,那么我们判断他是否过期,如果过期,删除session
else if(isset($_SESSION[$this->sessionName]["startTime"]) && time()-$_SESSION[$this->sessionName][&#39;startTime&#39;]>=$this->time)
{
unset($_SESSION[$this->sessionName]);
}
}
}
 ?>
Copy after login

Through this class we can achieve several purposes:
              1. We can clearly control the expiration time of the session.
                                                                                                                                                                                                               When I logged out as a user, I usually wrote session_destroy() like this; or unset($_SESSION); but I don’t know that this clears all sessions, so we will After the previous user exited, our own session was also deleted.
                3. Users will not be disconnected for no reason, because every step is now transparent to us.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1074128.htmlTechArticlephp Strictly control the expiration time of session After working for a while, I believe everyone must have encountered a problem: 1. The front-end user automatically disconnected for some reason. 2. After logging out from the back-end...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How much does a Douyin level 10 light sign cost? How many days does it take to create a level 10 fan sign? How much does a Douyin level 10 light sign cost? How many days does it take to create a level 10 fan sign? Mar 11, 2024 pm 05:37 PM

On the Douyin platform, many users are eager to obtain level certification, and the level 10 light sign shows the user's influence and recognition on Douyin. This article will delve into the price of Douyin’s level 10 light boards and the time it takes to reach this level to help users better understand the process. 1. How much does a level 10 Douyin light sign cost? The price of Douyin's 10-level light signs will vary depending on market fluctuations and supply and demand. The general price ranges from a few thousand yuan to ten thousand yuan. This price mainly includes the cost of the light sign itself and possible service fees. Users can purchase level 10 light signs through Douyin’s official channels or third-party service agencies, but they should pay attention to legal channels when purchasing to avoid false or fraudulent transactions. 2. How many days does it take to create a level 10 fan sign? Reach level 10 light sign

Can linux reset system time? Can linux reset system time? Mar 13, 2023 am 10:50 AM

Linux can reset the system time. The reset method is: 1. Use the date command to check the time; 2. Use the "yum install ntp" command to install ntp; 3. Use the "ntpdate -u ntp.api.bz" command to implement network time Just sync.

How long does it take to clear the Elden Ring? How long does it take to clear the Elden Ring? Mar 11, 2024 pm 12:50 PM

Players can experience the main plot of the game and collect game achievements when playing in Elden's Circle. Many players don't know how long it takes to clear Elden's Circle. The player's clearance process is 30 hours. How long does it take to clear the Elden Ring? Answer: 30 hours. 1. Although this 30-hour clearance time does not refer to a master-like speed pass, it also omits a lot of processes. 2. If you want to get a better game experience or experience the complete plot, then you will definitely need to spend more time on the duration. 3. If players collect them all, it will take about 100-120 hours. 4. If you only take the main line to brush BOSS, it will take about 50-60 hours. 5. If you want to experience it all: 150 hours of base time.

Why does my Go program take longer to compile? Why does my Go program take longer to compile? Jun 09, 2023 pm 06:00 PM

In recent years, Go language has become the choice of more and more developers. However, compared to other programming languages, the compilation speed of Go language is not fast enough. Many developers will encounter this problem when compiling Go programs: Why does my Go program take longer to compile? This article will explore this issue from several aspects. The compiler architecture of Go language The compiler architecture of Go language adopts a three-stage design, which are front-end, middle layer and back-end. The front-end is responsible for translating the source code into intermediate code in Go language, and the middle layer will

How to remove hours, minutes and seconds from time in php How to remove hours, minutes and seconds from time in php Mar 13, 2023 am 11:20 AM

How to use PHP to remove hours, minutes and seconds from time: 1. Create a PHP sample file; 2. Use the strtotime function to convert the date and time into a timestamp; 3. Use the date function to format the date or time to remove the hours, minutes and seconds.

How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? Mar 24, 2024 pm 01:31 PM

Xiaohongshu, a platform full of life and knowledge sharing, allows more and more creators to express their opinions freely. In order to get more attention and likes on Xiaohongshu, in addition to the quality of content, the time of publishing works is also crucial. So, how to set the time for Xiaohongshu to publish works? 1. How to set the time for publishing works on Xiaohongshu? 1. Understand the active time of users. First, it is necessary to clarify the active time of Xiaohongshu users. Generally speaking, 8 pm to 10 pm and weekend afternoons are the times when user activity is high. However, this time period will also vary depending on factors such as audience group and geography. Therefore, in order to better grasp the active period of users, it is recommended to conduct a more detailed analysis of the behavioral habits of different groups. By understanding users’ lives

Detailed explanation of Linux file time viewing techniques Detailed explanation of Linux file time viewing techniques Feb 21, 2024 pm 01:15 PM

Detailed explanation of Linux file time viewing techniques In Linux systems, file time information is very important for file management and tracking changes. The Linux system records file change information through three main time attributes, namely access time (atime), modification time (mtime) and change time (ctime). This article details how to view and manage this file time information, and provides specific code examples. 1. Check the file time information by using the ls command with the parameter -l to list the files.

How to use the time and date modules in Python How to use the time and date modules in Python Oct 16, 2023 am 08:11 AM

How to use the time and date modules in Python Introduction: In programming, dealing with time and dates are very common tasks. Python provides powerful time and date modules, making time and date operations easier and more convenient. This article will introduce the time and date modules in Python and provide specific code examples to help readers better understand and apply them. 1. Introducing the time and date module Python’s built-in time and date module is the datetime module. We need to introduce this module first.

See all articles