2014-01-06 21 views
0

我的問題是:'當我輸入錯誤的用戶名 - 密碼組合,它仍然將我重定向到索引頁,它應該被重定向到重新登錄頁面」 ..這是什麼問題?PL幫我...我atteching我的代碼...CakePHP:AUTH-> LOGIN()...無論用戶/通過錯誤...它重定向到索引頁

這裏的AppController:

class AppController extends Controller { 


     public $components=array('DebugKit.Toolbar', 
     'Session','Auth' => array(
      'loginRedirect' => array('controller' => 'users', 'action' => 'login'), 
      'logoutRedirect' => array('controller' => 'users', 'action' => 'index') 
     )); 


     public function beforeFilter(){ 

      $this->Auth->allow('index','register'); 
     } 
} 

這裏是UsersController:

class UsersController extends AppController 
{ 
    public $name='Users'; 
    public $uses=array('user'); 
    public $helpers = array('Html', 'Form','Session'); 

     public function beforeFilter() { 
     parent::beforeFilter(); 
     } 
     public function index() 
     { 

     } 
     public function login() { 
     if ($this->request->is('post')) { 
      /* login and redirect to url set in app controller */ 
      if ($this->Auth->login($this->request->data)) { 
       $this->Session->setFlash(__('Successful!!!')); 
       $this->Session->write('user',$this->data['user']['username'],time()+3600); 
       return $this->redirect(array('action'=>'index')); 
      } 
      $this->Session->setFlash(__('Invalid username or password, try again')); 
     } 
     // else{ echo "fail";exit;} 
    } 

    public function logout() { 
     /* logout and redirect to url set in app controller */ 
     $this->Session->destroy(); 
     return $this->redirect($this->Auth->logout()); 
    } 
public function register() { 
     if ($this->request->is('post')) { 
      $this->user->create(); 
      if ($this->user->save($this->request->data)) { 
       $this->Session->setFlash(__('The user has been saved')); 
       return $this->redirect(array('controller' => 'users','action' => 'index')); 
      } 
      $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
     } 


     } 
} 

這裏是用戶型號:

<?php 
class User extends AppModel 
{ 
     var $name='User'; 
     var $validate=array(
     'username'=> array(
       'rule'=>'notEmpty', 
        'message'=>'Enter Your USername'), 
     'password'=>array(
       'rule'=>'notEmpty', 
       'message'=>'Enter your Password' 
         ), 
     'Confirm_password'=>array(
       'rule'=>'match', 
       'message'=>'password not match, try again' 
         ) 

     ); 
     public function match(){ 
      if($this->data['user']['password']===$this->data['user']['Confirm_password']) 
      { 
       return true; 
      } 
      return false; 

     } 
     public function beforeSave($options = array()) { 

    /* password hashing */  
    if (isset($this->data[$this->alias]['password'])) { 
     $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); 
    } 
    return true; 
    } 

} 

?> 

這裏是我的登陸CTP:

<h2>LOGIN-MYSITE</h2> 
<?php echo $this->Form->create('user',array('action'=>'login')); 
echo $this->Form->input('username'); 
echo $this->Form->input('password'); 
echo $this->Form->end('LOGIN'); 


?> 
+0

'$這個 - > Auth->登錄($這個 - >請求 - >數據)'爲什麼所有人都明白這是它應該如何?它的錯誤 - 其文檔中包含一個大警告。 – mark

+0

@mark ...什麼是正確的方法? –

+0

@ndm在2.4.2中不支持???是嗎???? –

回答

0

刪除 '指數' 從

$this->Auth->allow('index','register'); 

保持

$this->Auth->allow('register'); 
+0

我想允許用戶查看'索引'頁面...正如你建議,它不會允許索引頁.... –

相關問題