php session_destroy()和session_unset()区别

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

session_unset()

PHP代码如下:

  1. <?php
  2. function session_clean1($logout=false)
  3. {
  4. $v=array();
  5. foreach($_SESSION as $x=>$y)
  6. if($x!="redirector"&&($x!="user"||$logout))
  7. $v[]=$x;
  8. foreach($v as $x)
  9. unset($_SESSION[$x]);
  10. return;
  11. }
  12. function session_clean2($logout=false)
  13. {
  14. foreach($_SESSION as $x=>$y)
  15. if($x!="redirector"&&($x!="user"||$logout))
  16. unset($_SESSION[$x]);
  17. return;
  18. }
  19. function session_clean3($logout=false)
  20. {
  21. $s=($logout||!isset($_SESSION["user"]))?array():
  22. array("user"=>$_SESSION["user"]);
  23. if(isset($_SESSION["redirector"]))
  24. $s["redirector"]=$_SESSION["redirector"];
  25. $_SESSION=$s;
  26. }
  27. ?>

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

session_destroy()

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

PHP代码如下:

  1. <?php
  2. // Initialize the session.
  3. // If you are using session_name("something"), don't forget it now!
  4. session_start();
  5. // Unset all of the session variables.
  6. $_SESSION = array();
  7. // If it's desired to kill the session, also delete the session cookie.
  8. // Note: This will destroy the session, and not just the session data!
  9. if (ini_get("session.use_cookies")) {
  10. $params = session_get_cookie_params();
  11. setcookie(session_name(), '', time() - 42000,
  12. $params["path"], $params["domain"],
  13. $params["secure"], $params["httponly"]
  14. );
  15. }
  16. // Finally, destroy the session.
  17. session_destroy();
  18. ?>