Home > Backend Development > PHP Problem > What to do if the PHP login session times out

What to do if the PHP login session times out

coldplay.xixi
Release: 2023-03-05 07:12:01
Original
2854 people have browsed it

The solution to the PHP login timeout session: First, use the session to record the login time when logging in; then when the page is opened, judge whether the session does not exist, and jump back to the login page; then, if the session exists, add the page loading time and Compare the login time; if it is greater than the timeout, delete it.

What to do if the PHP login session times out

[Related learning recommendations: php programming (video)]

php login timeout Session solution:

php login timeout session problem, the principle is:

1. Use session to record the login time when logging in

$_SESSION['time']=time();
Copy after login

2. When the page is opened Determine if the session does not exist, jump back to the login page; if the session exists, compare the page loading time with the login time, if it is greater than the timeout, delete the session, if the time is less than the timeout, update the login time session value!

Login code:

public function login(){
        if($_POST){
            $where['username']=$_POST['username'];
            $where['password']=md5("php100".$_POST['password']);
            $users=M('users')->where($where)->find();
            if($users){
                $_SESSION['id']=$users['id'];
                $_SESSION['user_shell']=md5($users['username'].$users['password']);
                $_SESSION['time']=time();
                $this->redirect('Index/index');
            }else{
                echo "";
            }
        }else{
            $this->display();
        }
    }
Copy after login

Public controller constructor code:

public function _initialize()
    {
        if(isset($_SESSION['user_shell'])){
            if(time()-$_SESSION['time']>60){
                unset($_SESSION['user_shell']);
                $url=U('Login/login');
                Header("Location:$url");
            }else{
                $_SESSION['time']=time();
            }
        }else{
            $url=U('Login/login');
            Header("Location:$url");
        }
    }
Copy after login

[Related learning recommendations: php graphic tutorial]

The above is the detailed content of What to do if the PHP login session times out. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template