在不使用Codeigniter的header.php中的对象上下文中时使用$ this

时间:2017-10-27 10:36:18

标签: php this codeigniter-3

我刚从在线获取一个代码来从db获取数据。它为我工作。但在使用phpstorm进行调试时。我收到了这个错误。我在header.php中使用$this来显示会话显示数据。

<li  class="dropdown" id="profile-messages" ><a title="" href="#" data-toggle="dropdown" data-target="#profile-messages" class="dropdown-toggle">
    <i class="icon icon-user"></i>  
    <span class="text">Welcome 
    <?php echo $this->session->userdata('user_name'); ?></span><b class="caret"></b></a>

我的控制员:

function login_user(){
  $user_login=array(

  'USER_NAME'=>$this->input->post('user_email'),
  'PASSWORD'=>($this->input->post('user_password')),
  'PIN'=>($this->input->post('user_pin'))
    );

    $data=$this->user_model->login_user($user_login['USER_NAME'],$user_login['PASSWORD'],$user_login['PIN']);

      if($data)
      {    
        $this->session->set_userdata('user_id',$data['USER_ID']);        
        $this->session->set_userdata('user_name',$data['USER_NAME']);
        $this->session->set_userdata('employee_id',$data['EMPLOYEE_ID']);
        $this->session->set_userdata('nationality',$data['NATIONALITY']);
        $this->session->set_userdata('EMPLOYEE_NAME',$data['EMPLOYEE_NAME']);
        $this->session->set_userdata('EMPLOYEE_NUMBER',$data['EMPLOYEE_NUMBER']);        
        $this->session->set_userdata('DATE_OF_BIRTH',$data['DATE_OF_BIRTH']);
        $this->session->set_userdata('JOINING_DATE',$data['JOINING_DATE']);
        $this->session->set_userdata('CURRENT_LOCATION',$data['CURRENT_LOCATION']);
        $this->session->set_userdata('SEX',$data['SEX']);
        $this->session->set_userdata('ADDRESS',$data['ADDRESS']);
        $this->session->set_userdata('COMPANY_EMPLOYEE_NUMBER',$data['COMPANY_EMPLOYEE_NUMBER']);
        $this->session->set_userdata('PHONE',$data['PHONE']);
        $this->session->set_userdata('MOBILE',$data['MOBILE']);
        $this->session->set_userdata('EMAIL_ADDRESS',$data['EMAIL_ADDRESS']);
        $this->session->set_userdata('ALTERNATE_EMAIL',$data['ALTERNATE_EMAIL']);
        $this->session->set_userdata('WORKING_HOURS',$data['WORKING_HOURS']);
        $this->session->set_userdata('NOK',$data['NOK']);
        $this->session->set_userdata('NOK_CONTACT_NUMBER',$data['NOK_CONTACT_NUMBER']);
        $this->session->set_userdata('EMPLOYMENT_STATUS_ID',$data['EMPLOYMENT_STATUS_ID']);
        $this->session->set_userdata('DESIGNATION_NAME',$data['DESIGNATION_NAME']);
        $this->session->set_userdata('CURRENT_ORG_ID',$data['CURRENT_ORG_ID']);
        $this->session->set_userdata('COS_NUMBER',$data['COS_NUMBER']);
        $this->session->set_userdata('VISA_START_DATE',$data['VISA_START_DATE']);
        $this->session->set_userdata('VISA_END_DATE',$data['VISA_END_DATE']);
        $this->session->set_userdata('DESCRIPTION',$data['DESCRIPTION']);
        $this->session->set_userdata('SKILLS',$data['SKILLS']);
        $this->session->set_userdata('YR_OF_EXPERIENCE',$data['YR_OF_EXPERIENCE']);



        $this->load->view('layouts/index.php');

      }
      else{
        $this->session->set_flashdata('error_msg', 'Error occured,Try again.');
        $this->load->view('layouts/login.php');

      }


}

1 个答案:

答案 0 :(得分:0)

实际上,您正在尝试访问始终无法访问的CodeIgniter类对象。请参考以下代码。

$CI =& get_instance();
echo $CI->session->userdata('username');

$ CI使用CodeIgniter类对象进行实例化,您将看到结果。