How to check if a session is established or not in Codeigniter?

I know how to create a session mainly in PHP, and I figured out how to do it in the codegamer, but I can’t figure out how to check if the session is established or not? I tried to check this via View, but it always gives meesesage Please Login.

Tell me how I can check if a Set or Not Set session is established.

controller

if ($user_type=='Student')
{
  if ($LoginData=   $this->loginmodel->studentLogin($username,$password))
  {
    foreach($LoginData as $UserId)
    {
      $currentId= $UserId->StudentId;
    }
    //[[session]]

    $data['students_data']=         $this->loginmodel->student_profile($currentId);

    $this->session->userdata('$data');

    $this->load->view('students',$data);
  }
  else
  { 
  //$data['message']= array('Invalid Username or Password');
    $this->load->view('Login');
    echo "Invalid Username or Password";
  }
}
elseif ($user_type=="Faculty")
{
  if($data['faculty_data']=$this->loginmodel->faculty_admin($username, $password))
  {
    $this->session->userdata('$data');

    $this->load->view('faculty');
  }
  else
  {
    $this->load->view('Login');
    echo "Invalid Username or Password";
  }
}

VIEW

<?php 

if (!$this->session->userdata('$data'))
{
    echo "Please Login";
}
else
{

}

?>

<!DOCTYPE
+4
source share
2 answers

Session Creation:

$newdata = array(
               'username'  => 'johndoe',
               'email'     => 'johndoe@some-site.com',
               'logged_in' => TRUE
           );

$this->session->set_userdata($newdata);

or

  $this->session->set_userdata('some_name', 'some_value');

But before that, make sure you have a session library.

$this->load->library('session');

Get session data:

if($this->session->userdata('whatever_session_name_is')){
     // do something when exist
}else{
    // do something when doesn't exist
}
+9
source
 if ($this->session->userdata('variable') !== FALSE) {
  echo 'Variable is set';
} else {
  echo 'Variable is not set';
}  

Additional Information

Demo code

+4
source

Source: https://habr.com/ru/post/1535908/


All Articles