php简单的登录登出

php by 黄业兴 at 2020-03-30

今天用PHP做了一个登录登出的功能

登录代码

if($_POST['password']=='xxxxx'){
        if (!session_id()) session_start();
        $_SESSION['password'] = 'xxxxxx';
        $this->getMessage("登录成功!",'/');
    }else{
        $this->getMessage("密码错误!",'/');
}

检查代码

if (!session_id()) session_start();
        $userName = '';
        if(isset($_SESSION['password'])&&$_SESSION['password']=='xxxxx'){
            $userName= array('id'=>'1','name'=>'admin');
        }
        if(!empty($userName)){
            $GLOBALS['user'] = $userName;
        }

登出代码

if (!session_id()) session_start();
        if(isset($_SESSION['password'])){
            unset($_SESSION['password']);
        }
        $this->getMessage("登出成功!",'/xxxx');


思路就是这样根据$GLOBALS['user']的值是否为空来判断用户是否登录。

请关注我们微信公众号:mw748219