2017-07-24 56 views
1

我試圖從數據庫中獲取用戶詳細信息並將其放入會話中,以便可以在視圖中使用它。我盡我所能,但我不斷收到錯誤。未定義的變量電子郵件。傳遞用戶信息以在codeigniter中查看

MODEL

function login($username, $password) 
    { 
     $this->db->select('authTbl.id, authTbl.password, authTbl.username, authTbl.email, authTbl.mobile'); 
     $this->db->from('users as authTbl'); 
     $this->db->where('authTbl.username', $username); 
     $this->db->where('authTbl.isDeleted', 0); 
     $query = $this->db->get(); 

     $user = $query->result(); 

     if(!empty($user)){ 
      if(verifyHashedPassword($password, $user[0]->password)){ 
       return $user; 
      } else { 
       return array(); 
      } 
     } else { 
      return array(); 
     } 
    } 

控制器

function isLoggedIn() 
    { 
     $isLoggedIn = $this->session->userdata('isLoggedIn'); 
     $data['title'] = 'Login'; 
     if(!isset($isLoggedIn) || $isLoggedIn != TRUE) 
     { 
      $this->load->view('templates/header'); 
      $this->load->view('users/login', $data); 
      $this->load->view('templates/footer'); 
     } 
     else 
     { 
      redirect('posts'); 
     } 
    } 

    /** 
    * This function used to logged in user 
    */ 
    public function login() 
    { 
     $this->load->library('form_validation'); 
     $data['title'] = 'Login'; 
     $this->form_validation->set_rules('username', 'Username', 'required|max_length[128]|trim'); 
     //$this->form_validation->set_rules('password', 'Password', 'required|max_length[32]|'); 

     if($this->form_validation->run() === FALSE) 
     { 
      $this->load->view('templates/header'); 
      $this->load->view('users/login', $data); 
      $this->load->view('templates/footer'); 
     } 
     else 
     { 
      $username = $this->input->post('username'); 
      $password = $this->input->post('password'); 

      $result = $this->user_model->login($username, $password); 

      if(count($result) > 0) 
      { 
       foreach ($result as $res) 
       { 
        $sessionArray = array('id'=>$res->id,      
              'username'=>$res->username, 
              'email'=>$res->email, 
              'mobile'=>$res->mobile, 
              'isLoggedIn' => TRUE 
            ); 

        $this->session->set_userdata($sessionArray); 
        $this->session->set_flashdata('user_login', 'Welcome'); 
        redirect('posts'); 
       } 
      } 
      else 
      { 
       $this->session->set_flashdata('login_fail', 'Email or password mismatch'); 

       redirect('users/login'); 
      } 
     } 
    } 

VIEW

<?php echo $email; ?> 
+0

您沒有通過'$ email'變量來查看'$ data' – Pritamkumar

+0

是的。使用會話 – King

+1

試試這個** $ this-> session-> userdata('email'); ** –

回答

2

利用這一點,因爲它已經被傳遞到會話

<?php echo $this->session->userdata('email'); ?> 
2

您所創建的會話FO [R用戶信息

嘗試獲取會話數據:

回聲$這個 - >會話級>用戶數據( '電子郵件');

或試圖傳遞的數據視圖$ DATA

0

如果您正在使用flashdata,那麼你可以得到它使用下面的代碼:

控制器

if (empty($this->session->userdata('UserID'))) { 
      $this->session->set_flashdata('flash_data', 'You don\'t have access!'); 
      $this->load->view("initialHeader"); 
      $this->load->view("login"); 
      redirect('Login'); 
     } 

查看

<?php if(!empty($this->session->flashdata('flash_data'))) { 
           ?> 
           <div class="alert alert-danger"> 
            <?php echo $this->session->flashdata('flash_data'); ?> 
           </div> 

          <?php } ?> 

如果您正在使用TMP數據會話,請參考下面的代碼:

控制器

<?php 
if (!empty($data)) { 
      $this->session->set_userdata('permission_error_msg', $data); 
      $this->session->mark_as_temp('permission_error_msg', 10); // For 10 Seconds 
      $this->load->view('dashboard', array($title, $data)); 
     } ?> 

查看

<?php if ($this->session->tempdata('permission_error_msg')) { ?> 
    <b class="login_error_msg"><?php 
     if (!empty($this->session->tempdata('permission_error_msg'))) { 
      echo $this->session->tempdata('permission_error_msg'); 
     } 
     ?></b> 
<?php } ?> 

謝謝。

2

$電子郵件將無法工作,因爲寫$電子郵件是指普通的變量,但在你的情況,你必須這樣寫:在你看來

<?php echo $this->session->userdata('email'); ?> 
相關問題