Home >Backend Development >PHP Problem >How to clear the specified session in php
In PHP, you can use the unset() function and the predefined variable "$_SESSION" to clear the specified session, the syntax is "unset($_SESSION['session name']);".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, clearing the specified session means deleting it $_SESSION
The specified element of the array;
Simply put, clearing the specified session is the same as the operation of the array. Simply log out of an element of the $_SESSION
array.
For example, when deleting $_SESSION['name']
, you can use the unset() function directly, such as unset($_SESSION['name'])
; .
The unset() function can release the specified variable. Its syntax format is as follows:
unset(mixed $var [, mixed $...])
where $var is the variable to be released. The unset() function can receive multiple parameters. Between the parameters Use,separate.
Note: When using the unset() function to delete a single Session element, be careful not to omit the specific element name, that is, do not unregister the entire $_SESSION array at once, as this may cause accidental Unexpected errors.
[Example] Use the unset() function to delete the specified Session element.
<?php session_start(); echo '<pre class="brush:php;toolbar:false">'; $str = 'PHP中文网'; $arr = ['删除 Session','$_SESSION']; $_SESSION['name'] = $str; $_SESSION['url'] = '//m.sbmmt.com/'; $_SESSION['title'] = $arr; echo '定义一个 Session,如下所示:<br>'; print_r($_SESSION); echo '删除 Session 中名为 title 的元素:<br>'; unset($_SESSION['title']); print_r($_SESSION); ?>
The running results are as follows:
定义一个 Session,如下所示: Array ( [name] => PHP中文网 [url] => //m.sbmmt.com/ [title] => Array ( [0] => 删除 Session [1] => $_SESSION ) ) 删除 Session 中名为 title 的元素: Array ( [name] => PHP中文网 [url] => //m.sbmmt.com/ )
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to clear the specified session in php. For more information, please follow other related articles on the PHP Chinese website!