4

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.

jasonaburton
  • 2,941
  • 7
  • 34
  • 48

2 Answers2

8

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...

ircmaxell
  • 163,128
  • 34
  • 264
  • 314
  • For example, could I get away with storing profile information in session? so name, age, gender, username, email, etc. – jasonaburton Feb 25 '11 at 20:21
  • @jason: sure. It's quite common to store a user object into the session. But I wouldn't store too much. Some text or a small object is fine, but I wouldn't store their publishing history... – ircmaxell Feb 25 '11 at 20:22
  • Oh I wasn't planning on storing their entire publishing history. Now, my question is how do I store an object in session? I've researched it and been told that I need to serialize and unserialize the object, but that hasn't worked for me. I don't know what I'm doing wrong. – jasonaburton Feb 25 '11 at 20:26
  • This is what I do - Page 1: $user->fname = "Jason Burton"; $_SESSION["user"] = serialize($user); Page 2: $user = unserialize($_SESSION["user"]); echo $user->$fname; – jasonaburton Feb 25 '11 at 20:28
  • It'll do it for you. Just `$_SESSION['foo'] = $obj`. Just make sure that the class is loaded before you start the session (or that you have an autoloader registered)... There's no need to serialize or unserialize since PHP will do that for you in the background – ircmaxell Feb 25 '11 at 20:29
  • I tried it, and I have the class loaded before hand but its still not working. I am going to edit my question and put some code up... – jasonaburton Feb 25 '11 at 20:31
  • What do you mean *it's still not working*??? *Not Working* is not a valid condition. Is it erroring? Is the value just not set? Is there something there, but it's cut off? What **exactly** is happening? – ircmaxell Feb 25 '11 at 20:34
  • The value is set in page one...how do I tell if the value is set in page 2 without getting the object out of session? I put some code up top. – jasonaburton Feb 25 '11 at 20:37
  • `var_dump($_SESSION['user']);` in the second page – ircmaxell Feb 25 '11 at 20:38
  • It comes up NULL. I checked the first page and right before I redirect to the profile page, I checked to see if the value was set and it was. So there was data in $_SESSION["user"] before I redirected. – jasonaburton Feb 25 '11 at 20:43
  • Oops, It actually came up with this: object(user)#1 (4) { ["username"]=> NULL ["fname"]=> NULL ["lname"]=> NULL ["jasonaburton"]=> string(12) "jasonaburton" } – jasonaburton Feb 25 '11 at 20:45
  • It's not storing it in the username portion of the object, even though I specifically tell it to... – jasonaburton Feb 25 '11 at 20:45
  • 2
    Change `$user->$username = 'jasonaburton'` to `$user->username = 'jasonaburton'` and you should be set... – ircmaxell Feb 25 '11 at 20:46
  • Yes! Thank you! (I'm very new to this, thanks for your patience) – jasonaburton Feb 25 '11 at 20:48
  • @ircmaxell `Don't use a session as a cache` _Techinally_ Sessions _should_ be used a cache - A cache for user data such as user names. You Definitely covered that in your answer correctly, though the statement on it not being cache may be be confusing. It's worth mentioning that _is_ a cache - Just not a cache for everything. – Super Cat Aug 13 '15 at 17:44
1

I would say :

  • If the data changes often, and each user needs to have an always up-to-date value, you'll probably want to query from database
  • If the date doesn't change, or changes don't need to be seen immediatly :
    • If the data is different for each user, you could store it in session (as the session is per-user)
    • If the data is the same for all users, you should use another caching mecanism (such as APC, or memcached), shared by all users, to avoid duplication


If storing to session, you should have to serialize/unserialize yourself : it's already done by the sessions mecanism (note that some data types cannot be serialized -- see Sessions).

If storing to cache (APC, memcached, files, ...), you'll often need to serialize/unserialize, because those caching mecanisms don't know how to store PHP objects.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663