我有一个名为“userinformations”的表。我想使用emailid和密码字段作为我的网站的登录凭据。 在相应的文件中添加了以下代码。但是当我登录时,我在同一个登录页面上,并显示一条消息“您无权访问该位置。”
应用/ app_controller.php
class AppController扩展Controller {
var $components = array('Auth','Session');
function beforeFilter() {
$this->Auth->userModel = 'Userinformation';
Security::setHash("md5");
$this->Auth->fields = array('username' => 'emailid', 'password' => 'password');
$this->Auth->loginAction = array('controller' => 'Userinformations', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'Userinformations', 'action' => 'updateuserprofile');
//sign up is a page which don't requires to login
$this->Auth->allow('signup');
$this->Auth->authorize = 'controller';
$this->Auth->logoutRedirect = '/';
}
function isAuthorized() {
return true;
}
}
型号:用户信息
class Userinformation extends Appmodel
{
var $name='Userinformation';
var $primaryKey = 'userid';
.
.
.
.
.
.
.
function hashPasswords($data){
if(isset($this->data['Userinformation']['password'])){
$this->data['Userinformation']['password']= md5($this->data['Userinformation']['password']);
return $data;
}
return $data;
}
function beforeSave() {
$this->hashPasswords(NULL,TRUE);
return TRUE;
}
}
控制器:UserinformationsController
function login(){
}
function logout() {
$this->redirect($this->Auth->logout());
}
查看:userinformations / login.ctp
<?php
echo $this->Session->flash('auth');
echo $form->create('userinformation', array('action' => 'login'));
echo $form->input('user.username');
echo $form->input('user.password');
echo $form->end('Login');
?>
答案 0 :(得分:0)
您不需要hashPassword(),并重写表单:
echo $form->create('userinformation', array('action' => 'login'));
echo $form->input('emailid');
echo $form->input('password');
echo $form->end('Login');