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

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

不言
不言forward
2019-03-09 13:39:561936browse

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";
?>

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");
    }  
?>

Logout

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

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete