PHP strictly controls the expiration time of the session

Source: Internet
Author: User
Tags exit ini key php class php session
Worked for some time, I believe you must have encountered a problem:          1. The front desk user does not know how to automatically drop the line.         2. All the front desk users are also off the line after the backstage exit.         3. I want to control my users half an hour automatic offline, found that changed the configuration file is not good.       All of the above issues are my recent encounter, and later through the query know: PHP session mechanism by several parameters at the same time control, specifically what I do not specifically write, is a probability, a maximum expiration time, There is also a storage path for the session. In php.ini we can see that the default expiration time for PHP's session is 24 minutes, which means that if we don't have an operation page for 24 minutes, this session expires. Of course this is the ideal state. PHP will start a session after 24 minutes. This mechanism is used to detect if the change time of the session file under the default storage directory is 24 minutes ago. If so, delete the session. Of course this is also the ideal state. This is one of the probabilities mentioned earlier, and the recycle mechanism of the session is triggered by probability, that is, even if your session is 24 minutes before the file, If the step triggers the recycle mechanism your session still does not expire. This is certainly not what we want. In order to solve this problem, I mentioned the third parameter, that is, the memory path of the session, if you do not open the php.ini internal session.save_ Path so session will not have file generation, so in order to be able to more effectively control the session we open and fill in a path, or in the file with Session_save_path ("...") function to define the path of the store for this session. And it's important that if the session is stored within our own defined path, the seesion recycle mechanism does not work. So we can only control the expiration time of the session.     Below is a I write on the basis of understanding of the expiration of the session of the processing class         
savepath = $savePath; 

Note: This must be written in front of the Session_Start Session_save_path ($this->savepath);  Session_Start ()//Open Session if (!is_dir ($this->savepath)) {//default to Maximum Permissions 0777 mkdir ($this->savepath) or
Die (' System error! '); ///create session altogether three parameters,//$name->session name//$val->session value///$t Ime-> expires, the default is 30 minutes 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][' VAl '] = $this->sessionvalue;
} $_session[$this->sessionname]["starttime"] = time (); //This time the session already exists, then we determine if he is expired and if it expires, delete session else if (Isset ($_session[$this->sessionname]["StartTime") & & Time ()-$_session[$this->sessionname][' starttime ']>= $this->time) {unset ($_session[$this->
SessionName]); }}}?>
We can achieve several goals through this class: 1. We can explicitly control the expiration time of the session. 2. Corresponding to the second problem, I used to do when the user out of the landing usually write Session_destroy (), or written as unset ($_session), but this will be all the session cleared, so we will encounter a previous user exit,                     Our own session has also been deleted. 3. The user will not drop the line for no reason, because each step is now transparent to us.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.