登录 php 时的用户 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10041196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
userid when logged in php
提问by user1295105
How can you get the userid="" in the URL when the user is logged in. I have mysql database with table users. I wan to know the code which basically knows what is the id of the user that has logged in. And using sessions but not sure about the code.
当用户登录时,如何在 URL 中获取 userid=""。我有带有表用户的 mysql 数据库。我想知道代码,它基本上知道登录的用户的 id 是什么。使用会话但不确定代码。
回答by Dr.Kameleon
Why don't you store the user's id in a session variable?
为什么不将用户的 id 存储在会话变量中?
// Get it from the db, after login
$_SESSION['user_id'] = $userid;
Example :
例子 :
A simple Login Script (storing userID, etc into Session Variables)
回答by fmask
$login_record = $YourLoginClassObject->Validate($email, $password);
if ($login_record != NULL ) //user record exist
{
$row = mysql_fetch_array($login_record);
$_SESSION["userid"] = trim($row["id"]);
// also you can store other information in session variables, like
$_SESSION["first_name"] = $row["first_name"];
$_SESSION["last_name"] = $row["last_name"];
}
else
{
//user not exist
}
And after userid set in session you can retrieve it on any page using $_SESSION["userid"]
在会话中设置用户 ID 后,您可以在任何页面上使用 $_SESSION["userid"]
Happy Coding
快乐编码
回答by Sandeep Manne
Why you need userId in url, its creates security problem, if you pass userId and user changed the id in url then in backend you will end up doing the operation on wrong userId, always take the user details from session.
为什么你需要在 url 中使用 userId,它会产生安全问题,如果你传递 userId 并且用户更改了 url 中的 id,那么在后端你最终会在错误的 userId 上进行操作,总是从会话中获取用户详细信息。
See this answerfor explanation on how you can store user info to session
回答by Dion
You should store the userid in the session (for example ($_SESSION["userid"]))
您应该将用户 ID 存储在会话中(例如 ($_SESSION["userid"]))

