2012-05-17 99 views
0

我一直在創建一個簡單的登錄/註冊頁面。註冊頁面不會將信息傳遞到數據庫中,但是當我嘗試使用該信息登錄時,頁面會出現無效的密碼/用戶名。我一直在關注Andrew Perkins教程(cakephp 2.0 auth),我的代碼與他的代碼完全相同。頁面不會接受登錄信息

- 我的數據庫的用戶是我的所有數據都存儲

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

    } 

    } 
} 

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

- 登錄視圖

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

我檢查代碼..檢查如何ü在烏拉圭回合數據庫中插入密碼的加密格式,,以及如何您登錄查詢射擊..或嘗試讀取蛋糕預定AUTH ... –

+0

可以請你更具體一點,密碼在數據庫中散列。 – user1393064

回答

0

數據庫中把它作爲一個20個字符長的字符串,該網站將派遣一個40字符長的字符串,它不會正確保存在數據庫中。

使用debugkit插件,它的工作

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

歡呼聲,讓登錄工作的任何提示? – user1393064

+0

數據庫將其存儲爲20個字符長的字符串,該網站正在發送一個長度爲40個字符的字符串,它不會正確保存在數據庫中。 使用了debugkit插件,它工作 – user1393064