开发者

PHP Login System Problem On Sessions!

开发者 https://www.devze.com 2023-03-04 00:30 出处:网络
I have a few questions about sessions and login/logout systems. In my system, first I amchecking whether the user data(username and password) are correct or not. If so i am registering a session: $_S

I have a few questions about sessions and login/logout systems.

In my system, first I am checking whether the user data(username and password) are correct or not. If so i am registering a session: $_SESSION['loggedin'] = 1 then I assume is logged in and I always check whether $_SESSION['loggedin'] 1 or not.

However, I recently observe that after one of the users logged in, let's say they go to their page: /profile.php?u=newuser but when they are in their own page if they are happen to change the url to this: /profile.php?u=newuser2 my system assumes that newuser开发者_运维知识库2 is loggedin now :( How could i solve this problem? What would be the best and secure way to log users in?

And lastly, would following way work? Let's say I register $_SESSION['username'] = $username; In here $username data is retrieved from database. And in order to understand a user logged in or not I always retrive username from database and check $_SESSION['username'] == username. Would this be logical? Would always getting the username from database be efficient?


As the session data is stored on the server, there should be no problem with storing the username in $_SESSION and checking against it as you said.

It would be better to store the user id, but it's not a big deal to be honest. Unless you allow users to change their username of course.

profile.php should not have a $_GET variable deciding who to edit. It should automatically use the person logged in. i.e.

Instead of going

$username = $_GET['u'];

You should be using

$username = $_SESSION['username'];
0

精彩评论

暂无评论...
验证码 换一张
取 消