Home  >  Article  >  Backend Development  >  How to clear the specified session in php

How to clear the specified session in php

青灯夜游
青灯夜游Original
2021-10-12 19:12:182752browse

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']);".

How to clear the specified session in php

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 &#39;<pre class="brush:php;toolbar:false">&#39;;
    $str = &#39;PHP中文网&#39;;
    $arr = [&#39;删除 Session&#39;,&#39;$_SESSION&#39;];
    $_SESSION[&#39;name&#39;]  = $str;
    $_SESSION[&#39;url&#39;]   = &#39;//m.sbmmt.com/&#39;;
    $_SESSION[&#39;title&#39;] = $arr;
    echo &#39;定义一个 Session,如下所示:<br>&#39;;
    print_r($_SESSION);
    echo &#39;删除 Session 中名为 title 的元素:<br>&#39;;
    unset($_SESSION[&#39;title&#39;]);
    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!

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