6

I know how to create session in core PHP, and I have understand how to do this in codeigniter, but I am unable to understand how to check, if the session is set or not? I have tried to check this through View but it always give me the meesage Please Login.

Kindly tell me how can I check whether the session is Set or not Set

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
Robert
  • 10,403
  • 14
  • 67
  • 117
user3480644
  • 577
  • 4
  • 8
  • 23

3 Answers3

21

Creating Session:

$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 ensure that you have the session library included.

$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
}
Sujit
  • 790
  • 1
  • 13
  • 27
6
 if ($this->session->userdata('variable') !== FALSE) {
  echo 'Variable is set';
} else {
  echo 'Variable is not set';
}  

More info

Demo code

Ravi Patel
  • 5,121
  • 2
  • 25
  • 44
  • where to write this? in view or a controller? – – user3480644 Apr 10 '14 at 11:01
  • @ravi: your tutorial is great! but I have a problem with my login/logout(let's use your demo as an example), when I logged-in then log-out and try to click the back button in browser(it is logged-in, right?), then try adding message then the bug appeared. My question is, how to deal with that problem? it's the cache that causes the bug? but I tried this code too(clearing the cache):[LINK HERE!](http://stackoverflow.com/questions/20071226/back-button-to-be-disable-after-logout) – Waelhi Apr 29 '15 at 08:01
  • Thanks, this is the answer to the question, "How to check if the session was set" – Neri Dec 06 '17 at 13:08
  • This is the correct answer. The accepted answer returns true whether it is set or set and null/0/blank. The onyl way to test whether the value has ever been set is ===FALSE – Doug Wolfgram Jun 07 '19 at 00:25
4

Using has_userdata

    if ($this->session->has_userdata('username')) {

        return TRUE;       

    } else {

        return FALSE;     

    }
antelove
  • 3,216
  • 26
  • 20