Home > Article > Backend Development > How to write logout in php
How to log out in php: first release all [$_SESSION] variables currently created in the memory, the code is [session_unset]; then delete the session file corresponding to the current user and release the session id.
[Related learning recommendations: php graphic tutorial]
How to write a logout in php:
In the PHP program, after the login is completed, it will be stored in the session. When logging out, the session needs to be released. The corresponding code is as follows.
<?php session_start(); if(isset($_SESSION["uid"])) // 检测变量是否设置 { session_unset(); // 释放当前在内存中已经创建的所有$_SESSION变量,但是不删除session文件以及不释放对应的session id; session_destroy(); // 删除当前用户对应的session文件以及释放session id,内存中$_SESSION变量内容依然保留; } header("location:login.php"); // 重定向到登录界面 ?>
It should be noted that:
session_unset();
Release all $_SESSION## that have been created in the memory. #Variable, but does not delete the session file and does not release the corresponding session id;
session_destroy(); Delete the session file corresponding to the current user and release the session id, The contents of the
$_SESSION variable in the memory are still retained;
Related learning recommendations:php programming (video)
The above is the detailed content of How to write logout in php. For more information, please follow other related articles on the PHP Chinese website!