Home  >  Article  >  Backend Development  >  How to log out and clear session in php

How to log out and clear session in php

藏色散人
藏色散人Original
2021-09-16 09:20:242891browse

The implementation method of php logging out and clearing the session: 1. Clear the SESSION value; 2. Determine whether the client's cookie file exists. If it exists, set it to expire; 3. Clear the server's session file.

How to log out and clear session in php

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer

How to log out and clear the session in php?

PHP's most thorough logout, clear session and cookie codes

public function logout(){  
$_SESSION = array(); //清除SESSION值.  
          if(isset($_COOKIE[session_name()])){  //判断客户端的cookie文件是否存在,存在的话将其设置为过期.  
                setcookie(session_name(),'',time()-1,'/');  
            }  
                session_destroy();  //清除服务器的sesion文件  
}

When logging out, if the session or cookies are not cleared cleanly, it will often cause All sorts of fantastical questions. Especially when your session is stored on the server using a database driver. Use the above code to clean it completely.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to log out and clear session in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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