cakephp中的用户身份验证
有些人可能会觉得这个问题很愚蠢。但是我确实已经做了所有的谷歌搜索,阅读了cakephp文档,但仍然无法理解cakephp的身份验证机制。我已经尝试了我的代码,但仍然无法验证 我的每一个正确输入的错误我得到的错误是无效的用户名密码。 这是我的密码cakephp中的用户身份验证,php,cakephp,Php,Cakephp,有些人可能会觉得这个问题很愚蠢。但是我确实已经做了所有的谷歌搜索,阅读了cakephp文档,但仍然无法理解cakephp的身份验证机制。我已经尝试了我的代码,但仍然无法验证 我的每一个正确输入的错误我得到的错误是无效的用户名密码。 这是我的密码 //login.tpl <div class="users form"> <?php echo $this->Session->flash('auth'); ?> <?php echo $this->Fo
//login.tpl
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Please enter your username and password'); ?></legend>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
有谁能告诉我如何进行身份验证吗?我对cakephp不熟悉好吧,cakephp本身就可以处理身份验证,您不需要在
登录
函数中编写代码
app\u controller.php
<?php
class AppController extends Controller
{
var $components = array
(
'Auth',
'Session',
'RequestHandler',
'Email'
);
var $helpers = array
(
'Javascript',
'Form',
'Html',
'Session'
);
function beforeFilter()
{
$this->Auth->autoRedirect = true;
$this->Auth->authError = 'Sorry, you are not authorized to view that page.';
$this->Auth->loginError = 'invalid username and password combination.';
$this->Auth->loginAction = array
(
'controller' => 'users',
'action' => 'login',
'admin' => true
);
$this->Auth->logoutRedirect = array
(
'controller' => 'users',
'action' => 'logout',
'admin' => true
);
$this->Auth->loginRedirect = array
(
'controller' => 'users',
'action' => 'dashboard',
'admin' => true
);
$this->Auth->fields = array('username' => 'email', 'password' => 'password');
}
?>
<?php
class UsersController extends AppController
{
var $name = 'Users';
/*
do not forget to add beforeFilter function and inside this function call parent beforeFilter function.
*/
function beforeFilter()
{
parent::beforeFilter();
}
function admin_login()
{
}
function admin_logout()
{
$this->Session->destroy();
$this->Auth->logout();
$this->Session->setFlash(__('Yor are now Logged out Successfully', true), 'default',array('class'=>'alert alert-success'));
$this->redirect('/');
exit;
}
?>
users\u controller.php
<?php
class AppController extends Controller
{
var $components = array
(
'Auth',
'Session',
'RequestHandler',
'Email'
);
var $helpers = array
(
'Javascript',
'Form',
'Html',
'Session'
);
function beforeFilter()
{
$this->Auth->autoRedirect = true;
$this->Auth->authError = 'Sorry, you are not authorized to view that page.';
$this->Auth->loginError = 'invalid username and password combination.';
$this->Auth->loginAction = array
(
'controller' => 'users',
'action' => 'login',
'admin' => true
);
$this->Auth->logoutRedirect = array
(
'controller' => 'users',
'action' => 'logout',
'admin' => true
);
$this->Auth->loginRedirect = array
(
'controller' => 'users',
'action' => 'dashboard',
'admin' => true
);
$this->Auth->fields = array('username' => 'email', 'password' => 'password');
}
?>
<?php
class UsersController extends AppController
{
var $name = 'Users';
/*
do not forget to add beforeFilter function and inside this function call parent beforeFilter function.
*/
function beforeFilter()
{
parent::beforeFilter();
}
function admin_login()
{
}
function admin_logout()
{
$this->Session->destroy();
$this->Auth->logout();
$this->Session->setFlash(__('Yor are now Logged out Successfully', true), 'default',array('class'=>'alert alert-success'));
$this->redirect('/');
exit;
}
?>
您就完成了。好吧,cakephp本身就可以处理身份验证,您不需要在
登录
函数中编写代码
app\u controller.php
<?php
class AppController extends Controller
{
var $components = array
(
'Auth',
'Session',
'RequestHandler',
'Email'
);
var $helpers = array
(
'Javascript',
'Form',
'Html',
'Session'
);
function beforeFilter()
{
$this->Auth->autoRedirect = true;
$this->Auth->authError = 'Sorry, you are not authorized to view that page.';
$this->Auth->loginError = 'invalid username and password combination.';
$this->Auth->loginAction = array
(
'controller' => 'users',
'action' => 'login',
'admin' => true
);
$this->Auth->logoutRedirect = array
(
'controller' => 'users',
'action' => 'logout',
'admin' => true
);
$this->Auth->loginRedirect = array
(
'controller' => 'users',
'action' => 'dashboard',
'admin' => true
);
$this->Auth->fields = array('username' => 'email', 'password' => 'password');
}
?>
<?php
class UsersController extends AppController
{
var $name = 'Users';
/*
do not forget to add beforeFilter function and inside this function call parent beforeFilter function.
*/
function beforeFilter()
{
parent::beforeFilter();
}
function admin_login()
{
}
function admin_logout()
{
$this->Session->destroy();
$this->Auth->logout();
$this->Session->setFlash(__('Yor are now Logged out Successfully', true), 'default',array('class'=>'alert alert-success'));
$this->redirect('/');
exit;
}
?>
users\u controller.php
<?php
class AppController extends Controller
{
var $components = array
(
'Auth',
'Session',
'RequestHandler',
'Email'
);
var $helpers = array
(
'Javascript',
'Form',
'Html',
'Session'
);
function beforeFilter()
{
$this->Auth->autoRedirect = true;
$this->Auth->authError = 'Sorry, you are not authorized to view that page.';
$this->Auth->loginError = 'invalid username and password combination.';
$this->Auth->loginAction = array
(
'controller' => 'users',
'action' => 'login',
'admin' => true
);
$this->Auth->logoutRedirect = array
(
'controller' => 'users',
'action' => 'logout',
'admin' => true
);
$this->Auth->loginRedirect = array
(
'controller' => 'users',
'action' => 'dashboard',
'admin' => true
);
$this->Auth->fields = array('username' => 'email', 'password' => 'password');
}
?>
<?php
class UsersController extends AppController
{
var $name = 'Users';
/*
do not forget to add beforeFilter function and inside this function call parent beforeFilter function.
*/
function beforeFilter()
{
parent::beforeFilter();
}
function admin_login()
{
}
function admin_logout()
{
$this->Session->destroy();
$this->Auth->logout();
$this->Session->setFlash(__('Yor are now Logged out Successfully', true), 'default',array('class'=>'alert alert-success'));
$this->redirect('/');
exit;
}
?>
你已经完成了。我使用了管理路由,但你也可以使用登录和注销功能…..只需在过滤功能之前从app_控制器中删除admin=>true你想让我将登录功能保留为空吗?让我们使用管理路由,但你也可以使用登录和注销功能…..只需从app_控制器中删除admin=>true在筛选函数之前,您希望我将登录函数保留为空吗?让我们