0

Possible Duplicate:
Store it in Session or Query DB each page load?

Which is more efficient/secure, store queried user data as a session or requery the db on each page load saving results to a global variable so nothing is saved as a session?

I'm curious since the former is how I usually do it while the latter is how WP does it. This isn't a comparison but merely a use-case scenario for web apps.

Community
  • 1
  • 1
enchance
  • 29,075
  • 35
  • 87
  • 127
  • 2
    Word of advice from a professional: _Don't compare anything ever at all to how WordPress does it..._ :) – Michael Berkowski Dec 23 '12 at 18:41
  • What Michael Berkowski said. WP is not the place to learn professional programming! Neither is Dreamweaver! – Ray Paseur Dec 23 '12 at 18:43
  • 1
    Seriously though, it's entirely up to your need. Session data is secure in as much as your server's filesystem is secure (but then, so's the database). Reading from the session means reading from disk, while the DB data may be in memory. You really just need to benchmark it. – Michael Berkowski Dec 23 '12 at 18:43
  • 2
    This sort of thing gets asked often, but rarely gets good answers. This one is good: [Store it in Session or Query DB each page load?](http://stackoverflow.com/questions/5121987/store-it-in-session-or-query-db-each-page-load) – Michael Berkowski Dec 23 '12 at 18:44

1 Answers1

0

Here is what I do: Start PHP, filter external data. Run the query that creates your variables needed to build your web page or response document. Use those variables and write the output.

I would avoid global variables. Some day you will be working on a project with other programmers. Global variables are troublesome in that environment.

The information stored in the session can (and does) expire. If you don't really need the data in the session, go back to the data base. This is particularly true for shopping cart information where it may take a client a long time to decide to purchase. If the cart was in the session, your web site may lose the memory of what they wanted to buy!

Ray Paseur
  • 2,106
  • 2
  • 13
  • 18