简体   繁体   English

会话中的PHP登录系统问题!

[英]PHP Login System Problem On Sessions!

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. 如果是这样,我正在注册一个会话: $_SESSION['loggedin'] = 1那么我假设已登录,并且我始终检查$_SESSION['loggedin'] 1是否。

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 newuser2 is loggedin now :( How could i solve this problem? What would be the best and secure way to log users in? 但是,我最近观察到,其中一个用户登录后,假设他们进入了他们的页面: /profile.php?u=newuser但是当他们在自己的页面中时,如果碰巧将URL更改为此: profile.php?u=newuser2我的系统假设newuser2现在已登录:(我该如何解决此问题?什么是最好的和安全的用户登录方式?

And lastly, would following way work? 最后,按照以下方式行得通吗? Let's say I register $_SESSION['username'] = $username; 假设我注册了$_SESSION['username'] = $username; In here $username data is retrieved from database. 在这里,$ username数据是从数据库中检索的。 And in order to understand a user logged in or not I always retrive username from database and check $_SESSION['username'] == username . 并且为了了解用户是否登录,我总是从数据库中检索用户名并检查$_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. 由于会话数据存储在服务器上,因此将用户名存储在$ _SESSION中并按照您所说的进行检查应该没有问题。

It would be better to store the user id, but it's not a big deal to be honest. 存储用户ID会更好,但是说实话这没什么大不了的。 Unless you allow users to change their username of course. 当然,除非您允许用户更改其用户名。

profile.php should not have a $_GET variable deciding who to edit. profile.php不应具有$ _GET变量来决定编辑者。 It should automatically use the person logged in. ie 它应该自动使用登录的人。即

Instead of going 而不是去

$username = $_GET['u'];

You should be using 你应该使用

$username = $_SESSION['username'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM