Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

php - Store it in Session or Query DB each page load?

Which is the better route to go?

Should I store my object in session and pass it from page to page, or should I query the database each time the user migrates to another page in my web app?

If I should store my object in session, how would I go about doing that? I've tried doing it with serialize and unserialize but it is not working for me...

Thanks for any help!

EDIT: Here is some of my code

Page 1:
include "user.php";
session_start();
$user = new user();
$user->$username = "Jason";
$_SESSION["user"] = $user;
header("Location: profile.php");

Page 2:
include "user.php";
session_start();
$user = new user();
$user = $_SESSION["user"];
echo $user->$username;

No results.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Only store data in a session that's user-specific. Don't use a session as a cache. Bad things will come from that (like eating up tons of disk space due to duplication of data).

If it is user specific, then I'd store it in a session only if it's reasonably small and if you need it often (I wouldn't store anything bigger than 10kb or so in a session). If you don't need it too often, then don't store it.

If it's not user specific, then use a cache layer. You could use raw APC/Memcached, or you could use an abstraction layer such as Cache_Lite or Zend_Cache...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...