我在 cake php 上使用 Auth 组件,但该组件不起作用
我在 cake php 上使用 Auth 组件,但该组件不起作用。
我的表是:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) default NULL,
`password` varchar(100) default NULL,
`role` varchar(50) default 'www',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
我的UsersController 是:
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Session');
var $components = array('Session', 'Auth');
function beforeFilter() {
$this->Auth->login($this->data);
$this->set('aya',$this->data);
$this->Auth->loginAction= array ('controller'=>'users','action'=>'login');
$this->Auth->loginRedirect = array ('controller'=>'users','actions'=>'edit','1');
$this->Auth->deny('delete');
}
function login() {
}
function logout() {
$this->redirect($this->Auth->logout());
}
我的login.ctp 文件是:
if($session->check('Message.auth'))
$session->flash('auth');
echo $form->create('User', array ('action','login'));
echo $form->input('usrname');
echo $form->input('password', array ('type','password'));
echo $form->end('Login');
但这不起作用,
为什么? 谢谢你的帮助。
i use Auth component on cake php and this component don't work.
my table is :
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) default NULL,
`password` varchar(100) default NULL,
`role` varchar(50) default 'www',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
and my UsersController is :
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Session');
var $components = array('Session', 'Auth');
function beforeFilter() {
$this->Auth->login($this->data);
$this->set('aya',$this->data);
$this->Auth->loginAction= array ('controller'=>'users','action'=>'login');
$this->Auth->loginRedirect = array ('controller'=>'users','actions'=>'edit','1');
$this->Auth->deny('delete');
}
function login() {
}
function logout() {
$this->redirect($this->Auth->logout());
}
and my login.ctp file is :
if($session->check('Message.auth'))
$session->flash('auth');
echo $form->create('User', array ('action','login'));
echo $form->input('usrname');
echo $form->input('password', array ('type','password'));
echo $form->end('Login');
but this is not work
why?
thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
beforeFilter() 将在每个请求中调用。 $this->data 将包含该请求的 POST 数据,而不仅仅是登录信息。这是你想要的吗?
您设置loginRedirect删除??
echo $form->create('用户', array ('action'=>'登录'));
echo $form->input('用户名');
echo $form->input('password', array ('type'=>'password'));
我不知道你到底想要实现什么,所以我无法告诉你如何编写代码。阅读手册:http://book.cakephp.org/view/1250/Authentication
beforeFilter() will be called in every request. And $this->data will have the POST data of that request, not just the login information. Is that what you want?
You set loginRedirect to delete??
echo $form->create('User', array ('action'=>'login'));
echo $form->input('username');
echo $form->input('password', array ('type'=>'password'));
I don't know exactly what you want to achieve so I can't tell you how to write the code. Read the manual: http://book.cakephp.org/view/1250/Authentication
你能定义不工作吗?它给你一个错误?什么都不做?
我认为,由于您仅在用户控制器中添加身份验证组件,而不是在 appController 中,这将使该组件仅在用户操作内工作。由于您在 UsersController 中只有登录和注销,并且允许此功能,那么该组件将不执行任何操作,只需让您的用户/登录自动工作...
我的建议是您将其
移至您的 AppController,它应该可以工作
编辑:
再次阅读你的代码,我看到很多错误:
$this->Auth->login($this->data);
你不应该使用这个,阅读手册,那是为了通常手动登录基于 ajax 的,这将在您每次输入并执行所有操作的登录时获取任何发布数据...因此删除这一行$this->Auth->loginRedirect = array ('controller'=>' users','actions'=>'delete');
您是否在用户登录后发送删除操作?建议:尝试使用索引或其他东西。$this->Auth->deny('delete');
除非你把它放在 allowedActions 中或者在被拒绝之前使用allow('delete')...删除这一行,因为它是如有必要,除非另有说明,否则一切都会被拒绝。请阅读 这教程,以便您知道您缺少什么以及您有什么。它应该像这样工作:D
can you define not working? it gives you an error? does nothing?
I think that since you are adding the auth component in the users controller only and not in the appController this will make the component to ONLY work inside User actions. And since you have only login and logout in UsersController and this functions are allowed then the component would do nothing, just make your users/login automagically work...
My suggestion is that you move this
to your AppController, and it should work
EDIT:
Reading again your code i see a lot of errors:
$this->Auth->login($this->data);
You should not use this, read the manual, that is for MANUAL login, usually ajax based one, this will get any post data everytime you enter and do the login for all actions... so delete this line$this->Auth->loginRedirect = array ('controller'=>'users','actions'=>'delete');
are you sending the users after they login to a delete action? suggestion: try using index or something else.$this->Auth->deny('delete');
unless you put it in allowedActions or use allow('delete') before is already denied... remove this line since it is innecesary, everything is denied unless it is said otherwisePlease read this tutorial so you know what are you missing and what you have extra. it SHOULD work like this :D