Home > Backend Development > PHP Tutorial > Introduction to PHP session (Session) access restrictions (code example)

Introduction to PHP session (Session) access restrictions (code example)

不言
Release: 2023-04-05 14:46:01
forward
1947 people have browsed it

This article brings you an introduction (code example) about PHP session (Session) access restrictions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Login

<?php
    // 启动会话
    session_start();
    //  注册登陆成功的 admin 变量,并赋值 true
    $_SESSION["admin"] = true;
    echo "login success";
?>
Copy after login

Access restrictions

<?php
    //  启动会话,这步必不可少
    session_start();
    //  判断是否登陆
    if (isset($_SESSION["admin"]) && $_SESSION["admin"] === true) 
    {
        //echo "您已经成功登录";
        echo "login success";
    } 
    else 
    {
        //  验证失败,将 $_SESSION["admin"] 置为 false
        $_SESSION["admin"] = false;
        die("no permission");
    }  
?>
Copy after login

Logout

<?php 
    // 启动会话
    session_start();
    //  这种方法是将原来注册的某个变量销毁
    unset($_SESSION[&#39;admin&#39;]);
    //  这种方法是销毁整个 Session 文件
    session_destroy();
    echo "logout success";
?>
Copy after login

The above is the detailed content of Introduction to PHP session (Session) access restrictions (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template