我正在尝试使用Cake
PHP 2.0获得一个简单的登录表单…只是Auth,现在没有ACL.
我能够看到表单并输入电子邮件和密码,因为它们在数据库中,但我只是返回到表单并显示flash错误消息.这是我的代码:
AppController的:
class AppController extends Controller { function beforeFilter() { $this->Auth->userModel = 'Users'; $this->Auth->fields = array('username' => 'email','password' => 'password'); //have to put both,even if we're just changing one $this->Auth->loginAction = array('controller' => 'users','action' => 'login'); $this->Auth->loginRedirect = array('controller' => 'hotels','action' => 'dashboard'); $this->Auth->logoutRedirect = array('controller' => 'users','action' => 'login'); } }
login.ctp:
<?PHP echo $this->Form->create('User',array('action' => 'login')); echo $this->Form->input('email'); echo $this->Form->input('password'); echo $this->Form->end('Login'); ?>
UsersController:
class UsersController extends AppController { var $name = 'Users'; var $helpers = array('Html','Form'); var $components = array('Auth','Session'); function beforeFilter() { $this->Auth->allow("logout"); parent::beforeFilter(); } function index() { } //Redirects to login() function login() { if ($this->Auth->login()) { $this->redirect($this->Auth->redirect()); } else { $this->Session->setFlash(__('Invalid username or password,try again')); } } function logout() { $this->redirect($this->Auth->logout()); } } ?>
我很感激任何帮助.谢谢!
解决方法@H_502_21@
登录后会显示“无效的用户名或密码,再试一次”错误?
你应该检查几件事:
•$this-> Auth-> login()的输出是否与数据库中的信息相同?调试($this-> Auth-> login())以在提交表单后查看login方法中的输出.
•密码是否在数据库中正确散列?
•尝试使AuthComponent对所有控制器可用,而不仅仅是UsersController.
•不确定这是否有所不同,但请调用parent :: beforeFilter();在控制器的beforeFilter方法之前的任何其他内容之前.
编辑:
是否正在尝试根据电子邮件和密码进行验证.默认情况下,AuthComponent需要用户名和密码.您必须明确声明您希望通过$this-> Auth-> login()验证电子邮件和密码.这来自2.0文档:
public $components = array(
'Auth'=> array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
你应该检查几件事:
•$this-> Auth-> login()的输出是否与数据库中的信息相同?调试($this-> Auth-> login())以在提交表单后查看login方法中的输出.
•密码是否在数据库中正确散列?
•尝试使AuthComponent对所有控制器可用,而不仅仅是UsersController.
•不确定这是否有所不同,但请调用parent :: beforeFilter();在控制器的beforeFilter方法之前的任何其他内容之前.
编辑:
是否正在尝试根据电子邮件和密码进行验证.默认情况下,AuthComponent需要用户名和密码.您必须明确声明您希望通过$this-> Auth-> login()验证电子邮件和密码.这来自2.0文档:
public $components = array( 'Auth'=> array( 'authenticate' => array( 'Form' => array( 'fields' => array('username' => 'email') ) ) ) );