php session_destroy()和session_unset()区别

admin 阅读:101 2024-03-01

本文章简单的介绍一下关于session_destroy(),session_unset()区别说明,有需要的朋友可以参考一下。 

session_unset()

PHP代码如下:

<?php 
 
function session_clean1($logout=false) 
 { 
  $v=array(); 
  foreach($_SESSION as $x=>$y) 
   if($x!="redirector"&&($x!="user"||$logout)) 
    $v[]=$x; 
 
  foreach($v as $x) 
   unset($_SESSION[$x]); 
  return; 
 } 
 
function session_clean2($logout=false) 
 { 
  foreach($_SESSION as $x=>$y) 
   if($x!="redirector"&&($x!="user"||$logout)) 
    unset($_SESSION[$x]); 
  return; 
 } 
 
function session_clean3($logout=false) 
 { 
  $s=($logout||!isset($_SESSION["user"]))?array(): 
   array("user"=>$_SESSION["user"]); 
  if(isset($_SESSION["redirector"])) 
   $s["redirector"]=$_SESSION["redirector"]; 
  $_SESSION=$s; 
 } 
?> 


释放当前在内存中已经创建的所有$_SESSION变量,但不删除session文件以及不释放对应的session id

session_destroy()

删除当前用户对应的session文件以及释放session id,内存中的$_SESSION变量内容依然保留,因此,释放用户的session所有资源,需要顺序执行如下代码:

PHP代码如下:

<?php 
// Initialize the session. 
// If you are using session_name("something"), don't forget it now! 
session_start(); 
 
// Unset all of the session variables. 
$_SESSION = array(); 
 
// If it's desired to kill the session, also delete the session cookie. 
// Note: This will destroy the session, and not just the session data! 
if (ini_get("session.use_cookies")) { 
    $params = session_get_cookie_params(); 
    setcookie(session_name(), '', time() - 42000, 
        $params["path"], $params["domain"], 
        $params["secure"], $params["httponly"] 
    ); 
} 
 
// Finally, destroy the session. 
session_destroy(); 
?> 


声明

1、部分文章来源于网络,仅作为参考。
2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!