PH(p)EW!! Info overload!! Can you please explain a $_SESSION to me please?

by 3 replies
4
Hi,

I'm trying my hardest to understand sessions here. If you could see what my understanding of them is, then tell me where I'm going wrong, it would be a great help.

Here's how I understand an example of a session:

User fills out a form that inserts to a database (insert.php).
(In the background, a session starts that holds (e.g. $username) variable value)

#programming #$session #explain #info #overload #phpew
  • Hey Andy - the simplest way to view a SESSION variable is to think of it as a small, temporary, disposable storage bin. You create a SESSION variable and PHP will build the bin and put your data in it.

    The benefit over plain variables is that SESSIONS last from page to page, until you end the SESSION (or sufficient time has elapsed and the temporary storage has ended).

    SAME CONCEPT as a variable $username, except, different type of storage so it's available on different pages.

    Does that help any?


    Now - as far as when/why to use them. If you're looking up a database record, you may need to remember the ID of a record, you can put that in a SESSION. Or, you may use something very often so you can use a SESSION variable to keep from having to constantly query the database. Variables can ease database hits immensely.

    It's all about your strategy, goal, and algorithm.


    Hope that helps! Powerful stuff...make sure to read up on additional security issues when using SESSIONS.
    • [ 1 ] Thanks
    • [1] reply
    • kdm provided a good explanation. And I'll just add that a session works similar to the way a cookie works. The only differences are that a cookie is stored on the end users computer, while a session is stored on your website's server. And a session is only good for as long as the end user stays on your website. As soon as they leave your site, the session is destroyed (unlike a cookie, which can last weeks or months, etc).
      • [ 2 ] Thanks
  • Thank you guys, that was really helpful. Thanks again .

Next Topics on Trending Feed