2012-03-07 110 views
0

美好的一天!我現在將我的代碼從1.3遷移到2.0的CakePHP。我只想問,我該如何做這個代碼(從1.3)到2.0?下面是代碼:註冊並在cakephp2.0中登錄

function register() { 
if(!empty($this->data)) { 
     // unset unrequired validation rules 
     unset($this->User->validate['username']['check_user']); 

     // validate & save data 
     if($this->User->save($this->data)) { 
      $this->data['User']['Password'] = md5($this->data['User']['Password']);   
      $this->User->save($this->data); 
      // set Flash & redirect 
      $this->Session->setFlash('You have successfully registered.','default',array('class'=>'flash_good')); 
      $this->redirect(array('action'=>'login')); 
     } 
     else{ 
      //$this->Session->setFlash(__('The user could not be saved.' , true)); 
      //$this->redirect(array('action' => 'register')); 
     } 
    } 
} 

這裏是我嘗試的代碼,我試圖來解決:

public function register() { 
    if($this->request->is('post')) { 
    //unset($this->User->validate['username']['check_user']); 

     // validate & save data 

     //$this->data['User']['Password'] = md5($this->data['User']['Password']);  
     $this->request->data('User.Password', $this->request->data('User.Password'));  
     // $this->User->save($this->data); 
     // set Flash & redirect 
     if($this->User->save($this->request->data)) { 
      $this->Session->setFlash('You have successfully registered.','default',array('class'=>'flash_good')); 
      $this->redirect(array('action'=>'login')); 
     } 
     else{ 
      //$this->Session->setFlash(__('The user could not be saved.' , true)); 
      //$this->redirect(array('action' => 'register')); 
     } 
    } 
} 

該代碼可用於登錄,在1.3

function login() { 
    //echo $_SESSION['User']['auth']; 
    if(!isset($_SESSION['User']['id'])){ 
     if(!empty($this->data)) { 
      if(($user = $this->User->validateLogin($this->data['User'])) == true) 
      { 
      //print_r(md5($this->data['User']['password'])); 
       $user = $this->User->find('first',array('conditions'=>array('Username'=>$this->data['User']['Username'],'Password'=>md5($this->data['User']['Password'])))); 
       //print_r ($user); 
       if(!empty($user)){ 
        $_SESSION['User']['id'] = $user['User']['id']; 
        $_SESSION['User']['name'] = $user['User']['Name']; 
        $_SESSION['User']['auth'] = $user['User']['auth']; 
        $this->redirect(array('controller'=>'ads','action'=>'index')); 
       }else{ 
        $this->Session->setFlash('Username/Password not match'); 
        $this->redirect(array('action'=>'login')); 
       } 
      } 
     } 
    } 
     else{ 
     $this->Session->setFlash('Login First.'); 
     $this->redirect(array('controller'=>'ads','action'=>'index')); 

    } 
} 

,並在這裏取得是我在2.0的代碼,但它仍然無法正常工作。

public function login() { 

    if(!($this->Session->read('user_id'))){ 
     if($this->request->is('post')) {   
      //$user = $this->User->find('first',array('conditions'=>array('Username'=>$this->data['User']['Username'],'Password'=>md5($this->data['User']['Password'])))); 

       if(!empty($user)){ 
        $this->Session->write('user_id',$user['User']['id']); 
        $this->Session->write('name',$user['User']['Name']); 
        //$this->Session->write('name',$user['User']['Name']); 
        $this->redirect(array('controller'=>'ads','action'=>'index')); 
       }else{ 
        $this->Session->setFlash('Username/Password not match'); 
        $this->redirect(array('action'=>'login')); 
       } 

     } 
    }else{ 
     $this->redirect(array('controller'=>'ads','action'=>'index')); 
    } 
}//end login 

我希望有人迴應我的問題。提前致謝。

+0

關於我的問題的後續信息,我太擔心密碼的md5,這是我想解決的問題。謝謝 – bowmeow 2012-03-07 02:31:47

回答