I remember being confuesd on this topic for a while when I was trying to figure it out, but it all becomes clear once one understands sessions. (It's even better once you look at things in term of GET and POST requests by the browser but that may be overkill here)
Cookies
- a variable name and value pair that is stored on a client computer/browser. This pair can be set by a server page, and also automatically is sent to any other server pages. The server page can "see" if what that variable/pair is and take action accordingly.
Session
- in MOST cases it requires a cookie
- any data is associated on the server with in essense a variable/value pair. The variable is lets say PHPSESSION and the value some 32 character random string.
- so if your server sets this cookie on a users computer any other pages accessed on your site are provided with this cookie data.
- now a session is simply the mechanism of associating some other data on the server with the value of this cookie. You can store a name, email or ANY stuff associated with this value. It's really easy to make your own sessions by just having a database with a cookie value as a key and then then some text/information as the data associated with that key.
- so simply when a server page is hit, it looks at the users "cookie"/session key value , looks it up in a table, and extracts any data that is associated with it.
- the session support in PHP just happens to automate things for you. THis "data" that I mentioned is a serialized representation of variables, arrays, objects that are assigned to the $SESSION. If you set any variable/value pairs in the $SESSION array in your page, after your page is finished processing this $_SESSION array is serialized (serialized is just a text representation of the element names and values in the array) and stored in a place keyed by the "cookie" variable value. The cookie name in this case is PHPSESSION (by default).
- when a new page is opened and you execute session_start() , this simple looks at the cookie value for the the PHPSESSION variable that you passed, then looks up the data assosicated, unserializes it and makes those values available once again in the $_SESSION variable.
There is no magic to it. Sessions is just a mechanism to associate some data on the server to some key passed in by the client.
To answer your question of whether using sessions or just cookies.
By definition sessions is some date associated with a user for a shorted duration, or more particularly, a browser session. Typically the cookie that stores the PHPSESSION variable/value pair is set as a cookie for the duration of the browser window and not actually set in a file on the client PC. (or at least that is IMHO the general usage) You typically would set some other cookie, like just the username. Your "auth" function would first look for a valid php session and if not present it would look for this permenant user identification and then start a new session with more of that users data for the duration of his visit. (REMEMBER - this permenant/long term cookie should be something encrypted, or can also have some server association to determine the user. This is because a user can change his cookie to some other username value and this is bad.)