PHP strictly controls the expiration time of the session _php tutorial

Source: Internet
Author: User
Tags php session

PHP strictly controls the session expiration time


Work over a period of time, I believe we must have encountered a problem: 1. Front desk users do not know how to automatically drop the line. 2. After exiting the login, all front-end users are also dropped. 3. I want to control my users for half an hour automatic offline, found that changed the configuration file is not good to make. All of the above issues are my recent encounter, and later through the query know: PHP session mechanism by a few parameters at the same time control, specifically which I do not specifically write, is a probability, a maximum expiration time, there is a session of the storage path. In php.ini we can see , the default expiration time of PHP session is 24 minutes, that is, if we do not operate the page for 24 minutes, this session expires, of course, this is the ideal state. 24 minutes later PHP will start a session of the recycling mechanism, This mechanism is used to detect if the session file under the default storage directory is changed by 24 minutes before the session is deleted. Of course this is also the ideal state. This is a probability that is mentioned earlier, the session of the recovery mechanism is triggered by probability, that is, Even if your session is 24 minutes before the file, if the step trigger recovery mechanism your session is still not expired. This is certainly not what we want. In order to solve this problem, I mentioned the third parameter, that is, the session storage path, If you do not open php.ini internal Session.save_path then the session will not have a file, so in order to more effectively control the session we open it and fill in a path, or in a file with Session_save_ Path ("...") function to define the stored paths of this session. It's also important that if the session is stored inside our own defined path, The recovery mechanism of seesion is not working. So we can only control the expiration time of the session. Here's an out-of-date processing class that I wrote about the session according to my understanding.
 Savepath = $savePath; Note: This must be written in front of Session_Start Session_save_path ($this->savepath); Session_Start ();//Turn on sessionif (!is_dir ($this->savepath)) {//default to Maximum permissions 0777mkdir ($this->savepath) or die (' System error! ');}} Create a session altogether three parameters,//$name->session name//$val->session value//$time  Expiration time, 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 ();} At this point the SESSION already exists, then we determine whether he expires, if expired, delete Sessionelse if (isset ($_session[$this->sessionname]["StartTime"]) & & Time ()-$_session[$this->sessionname][' startTime ']>= $thisTime) {unset ($_session[$this->sessionname]);}}} ?>

Through this class we can achieve several purposes: 1. We can explicitly control the expiration time of the session. 2. In response to the second question above, I usually write Session_destroy () when the user exits the login, or written as unset ($_session); so we can get rid of all the sessions, so we will encounter the previous user exit, Our own session was also deleted. 3. The user does not drop the line for no reason, because each step is now transparent to us.

http://www.bkjia.com/PHPjc/1074128.html www.bkjia.com true http://www.bkjia.com/PHPjc/1074128.html techarticle PHP strictly control the expiration time of the session work over a period of time, I believe we must have encountered a problem: 1. Front desk users do not know how to automatically drop the line. 2. Background exit after landing ...

  • 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.