Home  >  Article  >  Backend Development  >  How to delete data in session in php

How to delete data in session in php

藏色散人
藏色散人Original
2019-01-11 09:58:355179browse

php deletes data in the session. We can achieve the deletion operation through specific functions such as session_destroy() in PHP.

How to delete data in session in php

First of all, we need to briefly understand what the http stateless protocol is?

HTTP stateless protocol means that the protocol has no memory ability for transaction processing. To maintain state on the server and share data across multiple pages, a PHP session is used. PHP sessions are a simple way to store data for a single user/client based on a unique session ID.

The session ID is usually sent to the browser through a session cookie. The ID is used to retrieve existing session data. If there is no session ID on the server, a new session is created and a new session ID is generated.

Then delete the data in the session. The solution code is as follows:

1,
'first_name' =>
'Ramesh', 'last_name' =>
'Kumar', 'status' =>
'active'];

//检查会话

if (isset($_SESSION['user_info']))
	{
	echo "logged In";
	}

// 从session中取消设置

unset($_SESSION['user_info']['first_name']);

// 销毁完整会话

session_destroy();


?>

Note: session_start() will create a new session or reuse an existing session. If a session ID is submitted via GET or POST, or using a cookie, the existing session will be reused.

isset() detects whether the variable has been set and non-NULL

unset() destroys the specified variable. The behavior of unset() in a function will vary depending on the type of variable you want to destroy.

session_destroy() destroys all data in the current session, but will not reset the global variables associated with the current session, nor will it reset the session cookie. If you need to use session variables again, you must call the session_start() function again.

This article is an introduction to the method of deleting session data in PHP. It is simple and easy to understand. I hope it will be helpful to friends in need!

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