简体   繁体   中英

save basic information in a cookie or get in mysql all the time?

I have some information like the user full name and the profile picture that I need to access this information all the time in mysql. (all pages that he visits)

I was thinking to get this information once when user login and store this in a COOKIE. So every page I get this information in this cookie to avoid mysql connection for this everytime. (if the cookie is not set or lost date, I check mysql).

is it a good solution or a no sense one? can I have problems doing this? anyone has tried some similar thing?

thank you friends!

EDIT: I don't want to store LOGIN information in a cookie, I have SESSION for this. Just want to set a cookie for irrelevant information, his profile picture, his full name... not login access data.

Just like Darren suggested, you should never use cookies for personal information - those are rather easy to be recovered from public computers. Besides, there's no real drawback of loading the MySQL every time, unless you're very tight on bandwidth. My suggestion would to use a script like this:

$sql = new mysql("localhost", "Some_user", "some_password", "some_db");
$handle = $sql -> query("SELECT * FROM users_or_something");
while ($row = $handle -> fetch_object()){
   $name = $row -> name;
   $img_url = $row -> img;
   //...

}

And then...

include "db_load.php";

in every page.

Or even better, include it in your login processing page, then use $_SESSION[] to pass it between your scripts.

For faster logins, you could however store some user ID as a cookie and then, if the user opts in for auto-login, look for it. In that case, make the login page (index.php)? look for it before displaying a login form.

From a purely technical point of view, you can store pretty much anything as a cookie, if you can retrieve it. There's nothing (technically) wrong with what you're suggesting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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