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

by newbim
3 replies
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)

DOES THIS FORM PAGE POST IT IN A session.php PAGE?

On the rest of the pages, do you just use the variable to find records on a database and then display them from there?

There are a ton of tutorials out there that tell you how to start a session, but I don't understand how it fits in with the rest of the script.

Any clarification would be great.

Thanks for your time,

Andy.
#$session #explain #info #overload #phpew
  • Profile picture of the author kdm5157
    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.
    {{ DiscussionBoard.errors[5812888].message }}
    • Profile picture of the author Brandon Tanner
      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).
      Signature

      {{ DiscussionBoard.errors[5812932].message }}
  • Profile picture of the author newbim
    Thank you guys, that was really helpful. Thanks again .
    Signature
    If what I said helps, let me know, throw me a 'thanks'.
    {{ DiscussionBoard.errors[5812942].message }}

Trending Topics