Jump to content

Setting session for checking if a user is logged in


sorenchr

Recommended Posts

Hi there, quick question:

 

For determining if a user is logged in or not, when he/she is surfing on your site, what would be the best way to check this?

 

Currently i have two sessions which i check, 'username' and 'loggedin', 'username' is of course the username, and 'loggedin' can have a value of either 1 or 0. Is this fine enough for security? Or is this an open invitation to session highjacking?

 

The users passwords are sha1 encrypted before they are stored in the db. I'm thinking of using a 'password' session (instead of 'loggedin'), and then match up 'username' and 'password' with the db instead of just checking if 'loggedin' is 0 or 1. Would this be a more secure solution?

 

Thanks for your time.

Link to comment
Share on other sites

The purpose of a logged_in session variable is to remember the logged in status of the user, so this is what should be used to manage logged in status, here's some code of mine where I set it after a successful login:

 

//set logged_in session variable to 1 to indicate logged in status

  $_SESSION['logged_in'] = 1;

 

That is the whole point of the logged_in session variable. 

 

Username and Password should be used to authenticate the user and then used to set the logged_in session variable on successful login. 

 

Rgds

Link to comment
Share on other sites

The purpose of a logged_in session variable is to remember the logged in status of the user, so this is what should be used to manage logged in status, here's some code of mine where I set it after a successful login:

 

//set logged_in session variable to 1 to indicate logged in status

   $_SESSION['logged_in'] = 1;

 

That is the whole point of the logged_in session variable. 

 

Username and Password should be used to authenticate the user and then used to set the logged_in session variable on successful login. 

 

Rgds

 

But wouldn't it be ridicously easy for someone to set a fake session of 'logged_in'='1', and then they would be able to browse around the secure sites freely?

Link to comment
Share on other sites

But wouldn't it be ridicously easy for someone to set a fake session of 'logged_in'='1', and then they would be able to browse around the secure sites freely?

 

I am not an expert in this area but my view would be no, it's not easy to fake the session variable logged_in.  That's because it's your php code that determines whether the logged_in session variable is set.  You will only set it after a successful login, so as long as your login code is robust, your site should be secure. 

 

What I am trying to say here is that a hacker can't just say, hey, I think I'll set a session variable of logged_in and give it a value of 1.  The only thing that can decide to do this is your code that is running on the server during the login routine, so as long as you make your login routine safe, your site should be safe too. 

 

Rgds

Link to comment
Share on other sites

Sorry, maybee I wasnt quite clear on my last post.

 

That is why I also check the ip in the database for the logged in user with the ip from the visiting user as well as the logged_in session variable.  On login store their ip and on each page, check it with the database.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.