-5

What's the difference? I know that session variables store information and let you use it across pages, but I've got a few questions: Where is the value stored? And what is the difference between a session variable and a normal variable?

This is in the context of PHP. I'm after an in-depth answer because I've not found alot of information about this online.

Bob Uni
  • 2,601
  • 5
  • 17
  • 13
  • 3
    have you checked php.net or google? it is well documented. I am less than compelled to write you a report reiterating what is already there. – NappingRabbit Dec 13 '12 at 18:04
  • 1
    Hi NappingRabbit, yes I did checked google and php.net for a while before asking this. I didn't find exactly what I was looking for. I'm sorry you don't feel compelled to answer my question. – Bob Uni Dec 13 '12 at 18:10
  • For "for where is the value stored": normal variable, it's stored in (the server's) memory; session variable, it's stored in either a temp file or, if configured to, in a database. A full list of session config params can be found here: http://php.net/manual/en/session.configuration.php – Jakob Jingleheimer Dec 13 '12 at 18:14

3 Answers3

3

Where is the value stored?

That depends on the PHP configuration. By default, session variables are serialized and written into a file on the server's file system. On each page view that starts the session, they are unserialized and accessible from the $_SESSION array. It's possible to override the default session handler so that you can store the variables elsewhere, such as a database.

Sessions work by storing a session ID (which is a unique identifier) as a cookie on the client's computer. Each time the client requests a page, the session ID cookie is sent along with the request, PHP picks up the session ID from the cookie, and then pulls the sesssion data that relates to said session ID.

what is the difference between a session variable and a normal variable?

Simply put, a session variable gets saved to a source (such as file system), this is how they can persist between page requests. A normal variable will only live until the execution of the script completes, it will then be destroyed.

MrCode
  • 63,975
  • 10
  • 90
  • 112
0

By default, session variables are stored in a file on your server. Which session points to a user is handled usually be a cookie set in the browser (or perhaps an ugly parameter in the query string, but this is not advisable).

There's a lot of information on how sessions work in the documentation.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
0

PHP stores session variables (with unique ids) in what are essentially server-site cookies. It then stores a cookie in the user's browser keeping track of the Session ID so it can be looked up later.

Cookies are basically text files containing encoded data for each variable.

Normal variables are just stored in temporary memory and are garbage collected when finished with.

EDIT: Also, see this answer for an alternative definition: PHP: Storing 'objects' inside the $_SESSION

Community
  • 1
  • 1
n00dle
  • 5,949
  • 2
  • 35
  • 48