2012-03-03 15 views
0

新しい登録とログインページを作成しようとしています。私はログインに問題があります。CakePHP 2.0:ログインフォームが動作しない

1)私はそれが成功したハッシュされ、DBに保存されたユーザ名とパスワードを登録した後、以下のコードを見つけてください:(すべてはケーキの規則に従っている)

User.php:

<?php 
App::uses('AuthComponent', 'Controller/Component'); 
class User extends AppModel { 
...... 
public function beforeSave() { 
    if (isset($this->data[$this->alias]['password'])) { 
     $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); 
    } 
    return true; 
} 
} 
?> 

UsersController.php:

public function add() { 
    if ($this->request->is('post')) { 
     $this->User->create(); 
     if ($this->User->saveAll($this->request->data)) { 
      $this->Session->setFlash(__('The user has been saved')); 
      $this->redirect(array('controller' => 'Posts','action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
     } 
    } 
} 

public function login() { 
    if ($this->Auth->login()) { 
     $this->redirect($this->Auth->redirect()); 
    } else { 
     $this->Session->setFlash(__('Invalid username or password, try again'), 'default', array(), 'auth'); 
    } 
} 

public function logout() { 
    $this->redirect($this->Auth->logout()); 
} 

login.ctp:

<fieldset> 
<?php echo $this->Session->flash('auth'); ?> 
</fieldset> 
<?php echo $this->Form->create('Users');?> 
<fieldset> 
<?php 
    echo $this->Form->input('username'); 
    echo $this->Form->input('password'); 
?> 
</fieldset> 
<?php echo $this->Form->end(__('Login'));?> 

デバッグ:(デバッグ(の$ this - >データ);)

のAppControllerから:

Array 
(
[Users] => Array 
    (
     [username] => vinodronold 
     [password] => vinodronold 
    ) 

) 

がUserControllerから:

Array 
(
[Users] => Array 
    (
     [username] => vinodronold 
     [password] => vinodronold 
    ) 

) 

問題:

私がアクセスするたび/ユーザー/ログインURL「無効なユーザー名またはパスワード、再試行」メッセージが表示されます。

また、正しいユーザー名とパスワードを入力してもログインできません。

助けてください。

ありがとうございます!!!近いそれを見て

+0

すべて正しく表示されます。パスワードが暗号化されたデータベースに保存されていることを確認しましたか? –

+0

はい。パスワードはハッシュ後に保存されます。 $ this-> data [$ this-> alias] ['password'] = AuthComponent :: password($ this-> data [$ this-> alias] ['password']);何かする設定がある、私は何かが不足している.. –

答えて

0

、これにログイン機能であなたのif文を変更します。

if (!empty($this->request->data['User']) && $this->Auth->login()) { 

これは、ページにアクセスしたときに取得インスタントエラーメッセージを修正します。

あなたのフォームにあなたのフォームが間違っていることが気付きました。モデルは単数形です。フォームの作成方法を次のように変更してください。

<?php echo $this->Form->create('User');?> 

これは両方の問題で有効です。

+0

うわ..それは今働いた!ありがとう、トン! –

+0

喜んで助けてください!ハッピーコーディング! –

関連する問題