Function name: session_unset()
Function function: session_unset() function is used to release the values of all variables in the current session.
Parameter description: No parameters.
Return value: This function does not return value.
Sample code:
<?php // 开启会话session_start(); // 设置会话变量$_SESSION['username'] = 'John'; $_SESSION['age'] = 25; // 输出会话变量的值echo '用户名:' . $_SESSION['username'] . '<br>'; echo '年龄:' . $_SESSION['age'] . '<br>'; // 清空会话变量session_unset(); // 输出会话变量的值,应该为空echo '用户名:' . $_SESSION['username'] . '<br>'; echo '年龄:' . $_SESSION['age'] . '<br>'; ?>
Version of usage: This function is available in PHP 4, PHP 5, and PHP 7.
Notes:
- The session_unset() function will only clear the variable values in the current session and will not destroy the session itself.
- If you need to destroy the entire session, you can use the session_destroy() function.
- After calling the session_unset() function, you can check whether the session variable exists through the isset() function.
- After calling the session_unset() function, the value of the session variable will be set to NULL.
- After calling the session_unset() function, the session_register_shutdown() function can be used to trigger the closing and storage of the session.
- After calling the session_unset() function, you can save the session data and close the session through the session_commit() function.
- Normally, it is recommended to call the session_unset() function before the script ends to free the value of the session variable.