Home >Backend Development >PHP Tutorial >How to delete data in session in php
php deletes data in the session. We can achieve the deletion operation through specific functions such as session_destroy() 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:
<?php // 开启会话 session_start(); //创建会话 $_SESSION['user_info'] = ['user_id' =>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!