0

簡単なログイン/登録ページを作成しています。登録ページはデータベースに情報を渡しますが、その情報でログインしようとすると、ページに無効なパスワード/ユーザー名が表示され続けます。Andrew Perkins のチュートリアル (cakephp 2.0 auth) に従っていますが、私のコードは彼のものと同じです。

--my database users は、すべてのデータが保存される場所です

-ユーザーコントローラー

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

2 に答える 2

0

データベースは 20 文字の長さの文字列として保存していましたが、Web サイトは 40 文字の長さの文字列を送信していたため、データベースに正しく保存されませんでした。

debugkitプラグインを使用し、機能しました

于 2012-05-19T16:53:39.020 に答える
0

add 関数に余分な括弧がありました...これを試してください

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.');
    }
}
于 2012-05-17T09:44:13.663 に答える