Home  >  Article  >  Backend Development  >  Correctly log out of PHP SESSION

Correctly log out of PHP SESSION

WBOY
WBOYOriginal
2016-08-08 09:26:071086browse

/*
1、每个页面都必须开启session_start()后才能在每个页面里面使用session。

2、session_start()初始化session,第一次访问会生成一个唯一会话ID保存在客户端(是基于cookie保存的),用户下次访问时,session_start()会检查有没有会话ID,如果有浏览器会带着这个会话ID过来(通过发送头文件传过来的,这个可以用ff浏览器看到)来确定客户端。

3、给于cookie的session会在客户端保存一个会话ID即session_id,这个可以通过打印cookie看到,这个session_id的键值为session_name,
session_id() == $_COOKIE[session_name()]

4、如果客户端禁用了cookie,则必须用url传递session_id即给予URL的SESSION

5、注销SESSION时不能用unset($_SESSION),可以使用$_SESSION = array()或则$_SESSION = null,正确注销session的方法如下:
*/

//正确的注销session方法:
//1开启session
session_start();

//2、清空session信息
$_SESSION = array();

//3、清楚客户端sessionid
if(isset($_COOKIE[session_name()]))
{
    setCookie(session_name(),'',time()-3600,'/');
}
//4、彻底销毁session
session_destroy();

 

The above introduces the correct way to log out PHP SESSION, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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