-1

I just got one code from online to fetch data from db. Its working for me. But in debugging with phpstorm. I got this error. I am using $this in header.php for display data with session.

<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>

my controller:

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');

      }


}
amit sutar
  • 23
  • 3
  • Sorry but isn't your answer in your question title? *"Using $this when not in object context"* ? Why not use the object you instanciated? $this is for internal class constructs. Instantiate your object first, and use that instance later on in your page. – Nic3500 Oct 27 '17 at 11:27
  • Maybe I'm not clear on where the error is occurring, but what is the problem you're having and the desired outcome? – kchason Oct 27 '17 at 12:56
  • Possible duplicate of [PHP Fatal error: Using $this when not in object context](https://stackoverflow.com/questions/2350937/php-fatal-error-using-this-when-not-in-object-context) – Progman Oct 27 '17 at 19:23

1 Answers1

1

Actually, you are trying to access CodeIgniter class object in view which is always inaccessible. Please refer the code below.

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

$CI is instantiated with the CodeIgniter class object and you will see the result.

BEingprabhU
  • 1,618
  • 2
  • 21
  • 28