一、登陆后台验证
1、登陆html配置提交地址:
<form action="{:U('Admin/Login/login')}" method="post" id="login">
2、Admin/LoginAction.class.php添加login()
Public function login() { if (!IS_POST) halt('页面不存在'); // echo $_SESSION['verify'] . '<br/>'; // echo md5($_POST['code']); // p($_POST); if (I('code', '', 'md5') != session('verify')) { $this->error('验证码错误'); } $username = I('username'); $pwd = I('password', '', 'md5'); $user = M('user')->where(array('username' => $username))->find(); if (!$user || $user['password'] != $pwd) { $this->error('账号或者密码错误'); } if ($user['lock']) $this->error('用户被锁定'); $data = array( 'id' => $user['id'], 'logintime' => time(), 'loginip' => get_client_ip() ); M('user')->save($data); session('uid', $user['id']); session('username', $user['username']); session('logintime', date('Y-m-d H:i:s'), $user['logintime']); session('loginip', $user['loginip']); $this->redirect('Admin/Index/index'); }
3、为了校验session信息判断登陆跳转
写一个CommonAction.class.php 重写 _initialize()方法
<?php Class CommonAction extends Action { Public function _initialize() { if (!isset($_SESSION['uid']) || !isset($_SESSION['username'])) {//如果没有检测到session中的uid或者username让他跳转到登陆页 $this->redirect('Admin/Login/index'); } } } ?>
4、IndexAction.class.php 继承CommonAction
这样首页跳转就会初始化判断是否通过登陆过来的而不是url跳转过来
5、写个退出方法在IndexAction.class.php
public function logout() { session_unset();//清除session session_destroy();//摧毁session $this->redirect('Admin/Login/index'); }
6、Index.html中
<a href="{:U('Admin/Index/logout')}" target="_self">退出</a>
详情请看源码