Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php sessions secure log in

My question is about creating a secure log in routine. After comparing the user name and password to the stored values I set a session variable called logged to true. Then as the user surfs around the web page I just check the logged variable for true or false to determine if the user should have access.

This is my first time creating something like this. Is this secure? I feel like there is something else that I should be doing to make sure that users are valid.

like image 926
James Andino Avatar asked May 10 '26 18:05

James Andino


2 Answers

Anyone that gets your session cookie, is able to login as you. If you bind a session to an ip address, it's a lot harder. But this can give you problems with people that have changing ip addresses. It's up to you to decide if that's worth the trouble.

like image 179
Ikke Avatar answered May 13 '26 08:05

Ikke


If you're not handling any kind of sensitive information and just trying to provide a personal user experience, what you're doing is fine. However, if you're truly concerned about security, there are several other approaches you can take. The first is to create a database table called "user_tokens" or something similar. When a user signs in, create a random key and store their ip address in the table associated with the key. Also, store that key in a cookie on the clients' machine. Anytime they try to do something sensitive, you can compare their ip address and key of the cookie to that of the database.

Research a little bit into Cross-Site-Scripting (XSS) and session hijacking. The method I've outlined above will really cut down on this.

like image 24
DexterW Avatar answered May 13 '26 08:05

DexterW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!