2011-11-30 47 views
0

我用這個控制器來登錄和註銷的用戶,我想顯示歡迎信息,並退出鏈接登錄的用戶名的問題是,當我嘗試登錄該消息apear我打印的用戶名登錄時

通知(8):未定義變量:結果[APP \視圖\用戶\ login.ctp,線4]

users_controller.php中

<?php 
# /controllers/users_controller.php 
# please note that not all code is shown... 
uses('sanitize'); 
class UsersController extends AppController { 
    var $name = 'Users'; 
    // Include the Email Component so we can send some out :) 
    var $components = array('Email','Auth','Recaptcha'); 
    var $helpers = array('Recaptcha'); 


// Allow users to access the following action when not logged in 
    function beforeFilter() { 
     $this->Auth->allow('register','activate','logout','login'); 
     $this->Auth->autoRedirect = false; 
    } 


     function login() { 

       // Check for incoming login request. 
      if ($this->data) { 

      // Use the AuthComponent's login action 
       if ($this->Auth->login($this->data)) { 

       // Retrieve user data 
$results = $this->User->find(array('User.username' => $this->data['User']['username']), array('User.active'), null, false); 

       // Check to see if the User's account isn't active 
        if ($results['User']['active'] == 0) { 
        // Uh Oh! 
        $this->Session->setFlash('Your account has not been activated yet!'); 
         $this->Auth->logout(); 
         $this->data['User']['password'] = null; 
        //if not active user 
       }else { 
    $this->set('users', $results); 
    $this->redirect(array('controller' => 'users', 'action' => 'login')); 

      } 
         } 
       } 

     } 



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

用戶/ login.ctp

<?php if ($this->Session->read('Auth.User')):?> 
<?php 
    echo "Welcome".'<br />' ; 
echo $results; 
echo $html->link('logout', array('action'=>'logout')); 
?> 
<?php else : ?> 
<div class="types form"> 
<?php echo $form->create('User');?> 
    <fieldset> 
     <legend><?php echo ('Please enter your username and password'); ?></legend> 
    <?php 
     echo $form->input('username'); 
     echo $form->input('password'); 
    ?> 
    </fieldset> 
<?php echo $form->end(('Login'));?> 
</div> 
<?php endif; ?> 

回答

0

AppController.php

function beforeFilter(){ 
    $this->set('username', $this->_usersUsername()); 
} 

function _usersUsername(){ 
    $users_username = NULL; 
    if($this->Auth->user()){ 
     $users_username = $this->Auth->user('username'); 
    } 
    return $users_username; 
} 

view.ctp

<?php if(isset($username)) :?> 
hello <?php echo $username; ?>! Welcome back. 
<?php endif; ?>