开发者

Store all users info on login

开发者 https://www.devze.com 2022-12-17 03:24 出处:网络
Is it best to store all the users 开发者_如何转开发information in session cookies like this on login? Instead of using alot of querys

Is it best to store all the users 开发者_如何转开发information in session cookies like this on login? Instead of using alot of querys

$_SESSION['id'] = mysql_result($result, 0, 'id');
$_SESSION['name'] = mysql_result($result, 0, 'name');
$_SESSION['email'] = mysql_result($result, 0, 'email');
$_SESSION['ip'] = mysql_result($result, 0, 'regip');
$_SESSION['groupid'] = mysql_result($result, 0, 'group_id');
$_SESSION['style'] = mysql_result($result, 0, 'style');

Or maybe just store ID in session then query on all page:

select * from users where id = $_SESSION['id']


If you're going to be using them a lot, sure. Although I'd be careful about storing all of the users data in the Session. Only what is necessary. Keep in mind that well-written queries are cheap on resources. Don't try to avoid connecting to the database altogether, just be sure that when you do it, you do it well.

You can test your memory-consumption if you like using memory_get_usage(). But note that session data is stored in a flat file on the system itself:

session_start();
echo memory_get_usage(); // 87744
$_SESSION["user"] = array(
    "name"    => "Jonathan Sampson",
    "id"      => 54680,
    "groupID" => 0285,
    "email"   => "example@somesite.com",
    "style"   => "background-color:#333;color:#f1f1f1"
);
echo memory_get_usage(); // 88344

So you can see that it's really very trivial to store this amount of data.

See also: Cache data in PHP SESSION, or query from db each time?


I'd create a (salted) hash for each user, saved in the db:

$salt = 'asdjnvdskflds';
$hash = md5($salt . $email);

Then store the hash in the session. That way, you can call the user data each time. This also has the benefit of not having to update the session if the user changes some of his/her data.

0

精彩评论

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