页面不接受登录信息


Page wont accept login information

我一直在创建一个简单的登录/注册页面。注册页面确实将信息传递到数据库中,但是当我尝试使用该信息登录时,页面不断出现无效的密码/用户名。我一直在遵循Andrew Perkins教程(cakephp 2.0 auth),我的代码与他的相同。

-

-我的数据库用户是存储我所有数据的地方

-用户控制器

<?php
class UsersController extends AppController{
    function index(){
        $this->User->recursive = 0;
        $this->set('users', $this->User->find('all'));
        }
    public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('add');
    }
    public function isAuthorize($user){
        if(in_array($this->action, array('edit',delete))){
        if($user['id'] !=$this->request->params['pass'][0]){
            return false;
        }
        }
        return true;
    }
    public function login(){
        if($this->request->is('post')){
            if($this->Auth->login()){
                $this->redirect($this->Auth->redirect());
            }else{
                $this->Session->setFlash('Your username/password was incorrect');
            }
        }
    }
    public function logout(){
    $this->redirect($this->Auth->logout());
    }
    function add(){
        $this->set('title_for_layout', 'Individual Registration');
        $this->set('stylesheet_used', 'style');
        $this->set('image_used', 'eBOXLogo.jpg');   
 if($this->request->is('post')){
 { $this->User->create(); 
 if ($this->User->save($this->request->data)) 
 { 
    $this->Session->setFlash('The user has been saved');  
 }
  else { $this->Session->setFlash('The user could not be saved. Please, try again.'); } 
  } 
  } 
  }
}

-应用程序控制器

<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
    public $components = array(
        'Session', 
        'Auth'=>array(
            'longinRedirect'=>array('controller'=>'users', 'action'=>'index'),
            'longoutRedirect'=>array('controller'=>'users', 'action'=>'index'),
            'authError'=>"You can't access this page",
            'authorize'=>array('Controller')
        )
    );
    public function isAuthorized($user){
        return true;
    }
    public function beforeFilter(){
    $this->Auth->allow('index','view');
    $this->set('logged_in', $this->Auth->loggedIn());
    $this->set('current_user',$this->Auth->user());
    }

}

-用户模块

<?php
class User extends AppModel {
    public $name = 'User';
    public $displayField = 'name';
    public $validate = array(
        'name'=>array(
            'Please enter your name.'=>array(
                'rule'=>'notEmpty',
                'message'=>'Please enter your name.'
            )
        ),
        'username'=>array(
            'The username must be between 5 and 15 characters.'=>array(
                'rule'=>array('between', 5, 15),
                'message'=>'The username must be between 5 and 15 characters.'
            ),
            'That username has already been taken'=>array(
                'rule'=>'isUnique',
                'message'=>'That username has already been taken.'
            )
        ),
        'email'=>array(
            'Valid email'=>array(
                'rule'=>array('email'),
                'message'=>'Please enter a valid email address'
            )
        ),
        'password'=>array(
            'Not empty'=>array(
                'rule'=>'notEmpty',
                'message'=>'Please enter your password'
            ),
            'Match passwords'=>array(
                'rule'=>'matchPasswords',
                'message'=>'Your passwords do not match'
            )
        ),
        'password_confirmation'=>array(
            'Not empty'=>array(
                'rule'=>'notEmpty',
                'message'=>'Please confirm your password'
            )
        )
    );
    public function matchPasswords($data) {
        if ($data['password'] == $this->data['User']['password_confirmation']) {
            return true;
        }
        $this->invalidate('password_confirmation', 'Your passwords do not match');
        return false;
    }
    public function beforeSave() {
        if (isset($this->data['User']['password'])) {
            $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        }
        return true;
    }
}
?>

-登录视图

<h2>Login</h2>
<?php
echo $this->Form->create();
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Login');
?>

你在添加函数中有额外的括号...试试这个

if ($this->request->is('post')) {
    $this->User->create();
    if ($this->User->save($this->request->data)) {
        $this->Session->setFlash('The user has been saved');
    } else {
        $this->Session->setFlash('The user could not be saved. Please, try again.');
    }
}
数据库将其

存储为 20 个字符长的字符串,网站发送的字符串为 40 个字符长,它不会正确保存在数据库中。

使用调试工具包插件,它可以工作