频道栏目
首页 > 资讯 > PHP框架 > 正文

CakePHP简单的用户认证案例

16-01-07        来源:[db:作者]  
收藏   我要投稿

注:在看本篇文章之前请参照:CakePHP引入基于正则表达式的基本用户检验 中的 数据库设计 和 User模型 。

需要做的3件事情:

认证用户的方式(通常为校验用户的标识,比如用户名/密码组合) 跟踪用户访问情况的方式(通常使用Session) 校验用户是否已经认证通过的方式(通常是和Session进行交互)

我们从如何验证那些试图访问系统的用户开始。通过认证的用户信息会被Cake Session Component存储在PHP session中。我们从session中取到用户信息后就可以判断哪些操作是该用户可以执行的。

登录操作

第一个要完成的是登陆的view和action。这能给用户提供一个登陆的入口,同时也为系统提供了处理用户信息判断是否可以访问系统的机会。使用HTML helper可以很简单的创建该Form,目录在/app/views/users/login.thtml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

The login credentials you supplied could not be recognized. Please try again.

 
url('/users/login'); ?>" method="post">
 
Username:
input('username', array('size' => 20)); ?>
 
 
Password:
password('password', array('size' => 20)); ?>
 
 
submit('Login'); ?>
 
 

对应这个简单的视图(view),还需要一个action(/users/login),目录在 /app/controllers/users_controller.php 。

login 视图应当收集用户登录信息并将其提交给 users 控制器。users 控制器应当查看这个用户是否已在数据库中,并检验密码是否正确。如果用户已经正确地登录,则把用户名写入会话并把用户发送到 index 操作。

代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
functionlogin()
{
//Don't show the error message if no data has been submitted.
$this->set('error', false);
 
// If a user has submitted form data:
if(!empty($this->data))
{
// First, let's see if there are any users in the database
// with the username supplied by the user using the form:
 
$someone=$this->User->findByUsername($this->data['username']);
 
// At this point, $someone is full of user data, or its empty.
// Let's compare the form-submitted password with the one in
// the database.
 
if(!empty($someone['User']['password']) &&$someone['User']['password'] ==$this->data['password'])
{
// Note: hopefully your password in the DB is hashed,
// so your comparison might look more like:
// md5($this->data['User']['password']) == ...
 
// This means they were the same. We can now build some basic
// session information to remember this user as 'logged-in'.
 
$this->Session->write('User',$someone['User']);
 
// Now that we have them stored in a session, forward them on
// to a landing page for the application.
 
$this->redirect('/users/knownusers');
}
// Else, they supplied incorrect data:
else
{
// Remember the $error var in the view? Let's set that to true:
$this->set('error', true);
}
}
}

还不是很坏:如果你写的简炼点,代码应该不会超过20行。这个action的结果有这样两种:
① 用户通过认证,将信息存入Session,并转向到系统首页
② 未通过认证,返回到登陆页面,并显示相关错误信息。

提示:

使用内置的 $this->User->findByUsername($your_username_variable_here) 在数据库中搜索用户 用 $this->Session->write(‘user’, $your_username_variable_here) 把用户的名称写到 Session 中

现在可以正常访问:http://127.0.0.1/framework/cake_1.2.5/users/login

访问校验

现在我们可以认证用户了,让我们使系统可以踢除那些不登陆就希望自己访问非公共内容的用户。
一种办法就是在controller中添加一个函数来检查session状态。
建立 /app/app_controller.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classAppControllerextendsController
{
functioncheckSession()
{
// If the session info hasn't been set...
if(!$this->Session->check('User'))
{
// Force the user to login
$this->redirect('/users/login');
exit();
}
}
}
?>

现在你拥有了一个可以确保未经登陆的用户不能访问系统受限内容的函数了,你可以在任意级别来控制,下面是一些例子:

强制所有action都必须经过认证

1
2
3
4
5
6
7
8
9
10
11
12
13
classIndexControllerextendsAppController
{
// Don't want non-authenticated users looking at any of the actions
// in this controller? Use a beforeFilter to have Cake run checkSession
// before any action logic.
 
functionbeforeFilter()
{
$this->checkSession();
}
}
?>

在单独的action中要求认证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
functionknownusers()
{
// But you only want authenticated users to access this action.
$this->checkSession();
 
$this->set(
'knownusers',
$this->User->findAll(
null,
array('id','username','first_name','last_name'),
'id DESC'
)
);
}

注:尚未登录的会自动跳转登录界面,http://127.0.0.1/framework/cake_1.2.5/users/login。

注销操作

logout 操作应当从会话中删除用户的用户名,并将用户转发到 login 操作。

1
2
3
4
5
6
7
8
9
10
11
functionlogout()
{
// Redirect users to this action if they click on a Logout button.
// All we need to do here is trash the session information:
 
$this->Session->delete('User');
 
// And we should probably forward them somewhere, too...
 
$this->redirect('/');
}

你已经掌握了一些基础知识,可以开始实现自定义或者高级功能。我建议整合Cake的ACL控制是个不错的开始。

相关TAG标签
上一篇:Yii安装使用教程(转)
下一篇:CakePHP 路由设置connect和parseExtensions
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站