Home  >  Article  >  Backend Development  >  How to delete and destroy Session in php

How to delete and destroy Session in php

PHPz
PHPzOriginal
2017-04-26 14:32:2119423browse

Methods to delete and destroy Session in php: After using a Session variable, you can delete it; when you complete a reply, you can also destroy it. If the user wants to exit the Web system, you need to It provides a logout function to destroy everything in the server.

How to delete and destroy Session in php

There are three main ways to delete a session: deleting a single session, deleting multiple sessions and ending the current session. Below are three ways to make a simple introduce.

(1)Delete a single session

Deleting a single session means deleting the variables of a single session. The same as the operation of the array, directly log out an element of the $_SESSION array, that is Can.

For example: $_SESSION['user'] variable, you can use the unset() function, the code is as follows:

unset( $_SESSION['user']);

Note: When using the unset() function, pay attention to the $_SESSION array The element cannot be omitted, that is, the entire array cannot be unregistered at one time. This will disable the function of the entire session. For example, the unset($_SESSION) function will destroy the global variable $_SESSION, and there is no way to restore it, and the user cannot register $_SESSION again. variable.

If you want to delete multiple or all sessions, you can use the following two methods.

(2)Delete multiple sessions

If you want to delete all the variables registered by a user in the Session, that is, delete multiple sessions and log out at one time All session variables can be implemented by assigning an empty array to $_SESSION. The code is as follows:

$_SESSION = array();

(3) End the current session

If The entire session has ended. You should first log out all session variables, then use the session_destroy() function to clear and end the current session, clear all resources in the session, and completely destroy the Session. The code is as follows:

session_destroy();

Relative to session_start() Function (create Session file), session_destroy() function is used to close the operation of Session (delete Session file), return TRUE if successful, return FALSE if failed to destroy Session data. However, this function will not release the variables related to the current Session, nor will it delete the Session ID saved in the client cookie.

PHP's default Session is based on Cookie. The Session ID is stored in the client's Cookie by the server. Therefore, when logging out of the Session, you also need to clear the Session ID saved in the Cookie, and this requires the use of the setcookie() function. To be done. In Cookie, the Cookie identification name that saves the Session ID is the name of the Session. This name is the value specified by the session.name attribute in php.ini. In a php script, the name of the Session can be obtained through the session_name() function. Delete the Session ID saved in the client cookie.

The previous explanation can be concluded that the deletion and logout process of Session requires several steps. The complete code will be provided through an example below. After running this script, you can close the Session and destroy all resources related to this session.

The code to completely destroy the Session is as follows:

<?php
  //开启 Session
  session_start();
  // 删除所有 Session 变量
  $_SESSION = array();
  //判断 cookie 中是否保存 Session ID
   if(isset($_COOKIE[session_name()])){
     setcookie(session_name(),&#39;&#39;,time()-3600, &#39;/&#39;);
  }
  //彻底销毁 Session
  session_destroy();
?>

Note: Use $_SESSION = array() to clear $_SESSIONAt the same time as the array, the contents of the Session file corresponding to this user on the server side are also cleared. When using the session_destroy() function, the Session file corresponding to this user on the server side is deleted.

Related topic recommendations: php session (including pictures, texts, videos, cases)

The above is the detailed content of How to delete and destroy 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