php 用户登录与注销
时间:2019-02-11
本文章向大家介绍php 用户登录与注销,主要包括php 用户登录与注销使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
之前虽然写过登录的功能,但是注销还没有认真的看过。特此记录
当点击登录时,把用户账号信息以变量的形式存在定义的作用域(session)。session代表一次会话,只要你不关闭当前浏览器他的值就会存在,默认20分钟左右。
// BisAccount变量名, $ret用户账号信息 ,赋值bis作用域
session(‘BisAccount’, $ret, ‘bis’);
// 删除 bis作用域
session( null,‘bis’);
//取值 在bis作用域,名为BisAccount的值
session(‘BisAccount’, ‘’, ‘bis’);
Login
// 如果是post请求,即从登录页面过来的数据 则判断用户名和密码是否正确
账号密码与随机生成的数字拼接,使用MD5加密
// 如果不是post,判断session中是否有登录信息
有 跳转到登录后的页面
没有 跳转到登录页面
class Login extends Controller
{ //登录
public function index()
{
if (request()->isPost()) {
$data = input('post.');
$ret = model('BisAccount')->get(['username'=>$data['username']]);
if(empty($ret) || $ret->status != 1){
$this->error('该用户不存在或者未审核通过');
}
$pass = $data['password'];
$ran = $ret['code'];
if(md5($pass."".$ran) != $ret['password']){
$this->error('密码错误');
}
model('BisAccount')->updateById(['last_login_time'=>time()],$ret->id);
//session保存用户信息 赋值bis作用域
session('BisAccount', $ret, 'bis');
return $this->success('登录成功',url('Index/index'));
} else {
$account = session('BisAccount','','bis');
if(!empty($account) && $account->id){
return $this->redirect(url('index/index'));
}
return $this->fetch();
}
}
//注销
public function logout(){
session( null,'bis');
$this->redirect(url('login/index'));
}
}
登录后的页面
Base
判断session中是否有BisAccount
有,说明登录状态
没有,重定向到登录页面
class Base extends Controller
{
public $account;
public function _initialize() {
// 判定用户是否登录
$isLogin = $this->isLogin();
if(!$isLogin) {
return $this->redirect(url('login/index'));
}
}
//判定是否登录
public function isLogin() {
// 获取sesssion
$user = $this->getLoginUser();
if($user && $user->id) {
return true;
}
return false;
}
public function getLoginUser() {
if(!$this->account) {
$this->account = session('BisAccount', '', 'bis');
}
return $this->account;
}
}
Index
主页 继承自 Base 。如果账号注销后,再次访问index/index,则会走Base中_initialize初始化方法,
判断bis作用域中是否有BisAccount从而跳转到登录页面,而不会访问到登录后的页面。
登录后,访问index/index 与 login/index 返回相同的页面。
class Index extends Base
{
public function index()
{
return $this->fetch('');
}
}
tip: 如果存入的Session名称为A,取Session时也要取A
存
session(‘BisAccount’, $ret, ‘bis’);
取
session(‘BisAccount’, ‘’, ‘bis’);
都是BisAccount
- C语言心得一
- .NET Core装饰模式和.NET Core的Stream
- Nginx反向代理,负载均衡,redis session共享,keepalived高可用
- 多行图片hover加边框兼容IE7+
- Debian JDK安装及配置
- Python With-As
- 用 TensorFlow 创建自己的 Speech Recognizer
- Shell利剑之xargs和time
- Shell利剑之export、read和history
- css sprite 调整大张图片中小图标的大小
- Learn Git One
- Docker系列教程04-Docker镜像常用命令
- Linux 系统优化
- Spring Cloud Edgware新特性之九:Sleuth使用MQ方式整合Zipkin
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- Android下Button实现图文混排效果
- android handler.post和handler.sendMessage的区别和联系
- 3小时带你开发一款商城类小程序(零基础入门小程序系列)
- Android 隐式Intent的实例详解
- Android编程使用加速度传感器实现摇一摇功能及优化的方法详解
- 微信小程序分页加载数据~上拉加载更多~小程序云数据库的分页加载
- TextView实现跑马灯效果 就这么简单!
- Android自带emoji表情的使用方法详解
- Android EditText实现输入表情
- Android编程实现使用Intent传输包含自定义类的ArrayList示例
- EditText实现输入限制和校验功能实例代码
- java零基础入门006~springboot实现多文件的上传(java多文件的上传)
- Android编程实现随机生成颜色的方法示例
- Android中将Bitmap对象以PNG格式保存在内部存储中的方法
- python入门012~使用python3爬取网络图片并保存到本地