我从 cakebook 中得到了这个例子,但它不起作用,这是很多人的情况。这里的问题是,无论我发送什么数据,这都会通过登录。我的意思是我的数据库中只有一条记录用户名:ric 和密码:123。
如果我以 asdasdasd/asdasdasdasd 的形式提交,它就像已注册一样通过。我的模型:
<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $name = 'Users';
public $useTable = "rma_web_users";
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
)
);
}
?>
我的控制器:
<?php
class UsersController extends AppController {
public $name = 'Users';
public $uses = array('User');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add');
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
}
?>
我的应用控制器:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'home', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'home', 'action' => 'index')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
}
和我的登录视图:
<div class="users_login">
<p class='rma_login gray_label'>RMA Login System</p>
<div class="form_container">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<?php
echo $this->Form->input('username',array(
'label' => false,
'type' => 'text',
'value' =>'Email',
'class' => 'entrada',
'onFocus' => "if(this.value=='Email') this.value=''",
'onBlur' => "if(this.value=='') this.value='Email'")
);
echo $this->Form->input('password',array(
'label' => false,
'type' => 'password',
'class' => 'entrada')
);
?>
<?php
$submit = array(
'label' => 'Log in',
'class' => array( 'boton_azul', 'login')
);
echo $this->Form->end($submit); ?>
</div>
</div>
太感谢了。