当前位置: 动力学知识库 > 问答 > 编程问答 >

session - userid when logged in php

问题描述:

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.

网友答案:

Why don't you store the user's id in a session variable?

// Get it from the db, after login

$_SESSION['user_id'] = $userid;

Example :

A simple Login Script (storing userID, etc into Session Variables)

网友答案:
$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"]

Happy Coding

网友答案:

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.

See this answer for explanation on how you can store user info to session

网友答案:

You should store the userid in the session (for example ($_SESSION["userid"]))

分享给朋友:
您可能感兴趣的文章:
随机阅读: