页面不会接受登录信息

时间:2012-05-17 09:33:02

标签: php cakephp login

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

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

-Users Controller

<?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.'); } 
  } 

  } 

  }
}

-App控制器

<?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;
    }
}
?>

-login view

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

2 个答案:

答案 0 :(得分:0)

你在添加功能中有额外的括号...尝试这个

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.');
    }
}

答案 1 :(得分:0)

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

使用了debugkit插件并且工作正常