php使用 _before_index() 来实现访问页面前,判断登录

时间:2023-03-10 04:17:03
php使用 _before_index() 来实现访问页面前,判断登录
C:\wamp\www\DEVOPS\Home\Lib\Action:

<?php
class IndexAction extends Action {
function index(){
$this->display();
} function do_login(){
//获取用户名和密码信息,和数据库中比对
echo 111111111;
dump($_POST);
dump($_SESSION);
echo 222222222;
$username=$_POST['username'];
$password=$_POST['password'];
$code=$_POST['code'];
#$this->display();
if($_SESSION['verify']!==md5($code)){
$this->error('验证码错误');
} $m=new Model('user');
$where['username']=$username;
$where['password']=md5($password); $arr = $m->where($where)->find(); $i=$m->where($where)->count(); if ($i>0){
$_SESSION['username']=$username;
$_SESSION['authority'] = $arr['authority'];
#$this->redirect('Main/index');
}else{
$this->error('该用户不存在');
}
} function checkUser(){
$username=$_POST['username'];
$m=new Model('user');
$where['username']=$username;
$i=$m->where($where)->count();
if ($i>0){
echo "1";
}else{
echo "0";
}
} function checkPasswd(){
$username=$_POST['username'];
$password=$_POST['password']; $m=new Model('user');
$where['username']=$username;
$where['password']=md5($password);
$i=$m->where($where)->count();
if ($i>0){
echo "1";
}else{
echo "0";
}
} function checkCode(){
$code=$_POST['code'];
if($_SESSION['verify']!==md5($code)){
echo "0";
}else{
echo "1";
}
}
}
?> Index模块下的index方法,调用前台模板 <html>
<head>
<title>Index</title>
<link rel='stylesheet' type='text/css' href='__PUBLIC__/Css/Index/index.css'/>
</head>
<body>
<h1>中均运维管理平台</h1>
<!--图片标签-->
<img class="img_bk" src="__PUBLIC__/Images/scan.jpg"/>
<!--表单提交-->
<form action='__URL__/do_login' method='post' name="myForm">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="td1">用户名:</td>
<td><input type="text" name="username"/></td>
<td class="td3"></td>
<td class="td4"></td>
<tr/>
<tr>
<td class="td1">密码:</td>
<td><input type="password" name="password"/></td>
<td class="td3"></td>
<td class="td4"></td>
<tr/>
<tr>
<td class="td1">验证码:</td>
<td> <input type='text' name='code' /></td>
<td class="td3"><img src="__APP__/Public/code" onclick='this.src=this.src+"?"+Math.random()'/></td>
<td class="td4"></td>
</tr>
<tr>
<td class="td1"></td>
<td><input type="button" value="" name="imgLogin"/></td>
<td class="td3"></td>
<td class="td4"></td>
</tr>
</table>
</form>
</body>
<script type="text/javascript" src="__PUBLIC__/Js/jquery-2.2.2.min.js"></script>
<script src="__PUBLIC__/Js/Index/index.js"></script>
<script src="__PUBLIC__/Js/Public/ajax.js"></script>
</html> 前台模板调用_URL__/do_login 等价于/DEVOPS/index.php/Index/do_login <form action="/DEVOPS/index.php/Index/do_login" method="post" name="myForm"> 那么怎么判断菜单访问页面前,判断登录呢? http://localhost/DEVOPS/index.php/Test/index 测试 此时可以直接访问: <?php
class TestAction extends Action{
// function _before_index(){
// if(!isset($_SESSION['username']) || $_SESSION['username']=='')
// {
// $this->redirect('Index/index');
// }
// } public function index(){
$this->display();
}
}; 此时跳过登录检查,那么怎么判断登录呢? //********************************* 前置和后置操作上一页下一页系统会检测当前操作是否具有前置和后置操作,如果存在就会按照顺序执行,前置和后置操作的方法名是在要执行的方法前面加 _before_和_after_,例如:
class CityAction extends Action{
//前置操作方法
public function _before_index(){
echo 'before<br/>';
}
public function index(){
echo 'index<br/>';
}
//后置操作方法
public function _after_index(){
echo 'after<br/>';
}
}
如果我们访问
http://serverName/index.php/City/index
结果会输出 before
index
after <?php
class TestAction extends Action{
function _before_index(){
if(!isset($_SESSION['username']) || $_SESSION['username']=='')
{
$this->redirect('Index/index');
}
} public function index(){
$this->display();
}
}; 此时会跳转到登录页 /*********** 打印信息: <?php
class TestAction extends Action{
function _before_index(){
var_dump($_SESSION);
#echo $_SESSION;
if(!isset($_SESSION['username']) || $_SESSION['username']=='')
{
#$this->redirect('Index/index');
}
} public function index(){
$this->display();
}
}; 此时访问http://localhost/DEVOPS/index.php/Test/index: array (size=1)
'verify' => string '8289889263db4a40463e3f358bb7c7a1' (length=32) test 20160707 scan! 可以看到 $_SESSION['username'] 为空 如果登录的话SESSION里的信息为: array (size=3)
'verify' => string '198dd5fb9c43b2d29a548f8c77e85cf9' (length=32)
'username' => string 'admin' (length=5)
'authority' => string '1' (length=1)