CodeIgniter Forums
How to verify if a user is logged in? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to verify if a user is logged in? (/showthread.php?tid=61496)



How to verify if a user is logged in? - lexxtoronto - 04-20-2015

Im using a session database. I set some custom data in the session:

Code:
$sessiondata=array('username'=>$username, 'loginuser'=>TRUE);
                    $this->session->set_userdata($sessiondata);
                    
                    $sessionId_local = $this->session->userdata('session_id');
                    
                    $this->db->where('session_id',  $sessionId_local);  
                    $this->db->update('ci_sessions',$sessiondata);

So I set username and loginuser, then enter these values in ci_sessions table matching the session_id so that it is for the same user.

So login kinda works and the user is redirected to admin.php. What if I go to admin.php directly how do I verify if the user is logged in and is allowed to access admin.php?

I want to match data against ci_sessions table.


RE: How to verify if a user is logged in? - paju89 - 04-20-2015

(04-20-2015, 12:51 PM)lexxtoronto Wrote: Im using a session database. I set some custom data in the session:


Code:
           $sessiondata=array('username'=>$username, 'loginuser'=>TRUE);
                   $this->session->set_userdata($sessiondata);
                   
                   $sessionId_local = $this->session->userdata('session_id');
                   
                   $this->db->where('session_id',  $sessionId_local);  
                   $this->db->update('ci_sessions',$sessiondata);

So I set username and loginuser, then enter these values in ci_sessions table matching the session_id so that it is for the same user.

So login kinda works and the user is redirected to admin.php. What if I go to admin.php directly how do I verify if the user is logged in and is allowed to access admin.php?

I want to match data against ci_sessions table.




i use after login this code to verify if user loged or not

Code:
if(($this->session->userdata('user_name')==""))
  {
     $this->welcome();   }
        else
        {..........................



RE: How to verify if a user is logged in? - lexxtoronto - 04-20-2015

Thanks. So if user_name in session cookie is empty then....?


RE: How to verify if a user is logged in? - silentium - 04-20-2015

Since you set both 'username' and 'loginuser' in your session, you can use both or either of them to check if the user is logged in.

Here is a quick example using 'username' for the check
PHP Code:
// Check if username exists in session
if ($this->session->userdata('username') === NULL)
{
 
   // User is not logged in, redirect to login screen
}
else
{
 
   // User is logged in, allow access




RE: How to verify if a user is logged in? - lexxtoronto - 04-20-2015

Thank you guys.


RE: How to verify if a user is logged in? - RogerMore - 04-21-2015

Hey lexxtoronto,

Are you still trying to update the ci_session table using $this->db->update('ci_sessions',$sessionData);?

I would strongly advise to use the session methods, instead of using db methods. The only time I would use db methods directly on ci_sessions, is when I want to find out which sessions are active in for example the last half hour by retrieving all records with a certain timestamp. For all other session related stuff I would use the session methods...

Happy coding!


RE: How to verify if a user is logged in? - lexxtoronto - 04-22-2015

Hi RogerMore, not really, turns out CI does it automatically when you use a db table for sessions. If there is a valid session cookie then CI will check it against the db and if there is no match then it will destroy the session cookie and generate a new one. I thought I had to do it myself, CI is taking care of it for me!! Thank you anyway!


RE: How to verify if a user is logged in? - madaan_tushar - 05-25-2015

You can verify,if the user is loggedin only by setting the session.