• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to verify if a user is logged in?

#1
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.
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply

#2
(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
        {..........................
Reply

#3
Thanks. So if user_name in session cookie is empty then....?
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply

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

Reply

#5
Thank you guys.
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply

#6
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!
Reply

#7
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!
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply

#8
You can verify,if the user is loggedin only by setting the session.
Rolleyes
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.