Sessions are handy when you want to remember some information(i.e. variables ) and keep it accessible across website's pages.
What are they being used for?
Usually to remember some preferences like:
- user name
- shopping list of items
- user rights: like what kind of permissions does one have
- user state: whether someone is logged in or not
Session and cookies - differences
2. Cookies data is browser stored, while sessions data reside on the web server being invisible to the end user.
How to work with sessions?
You'll just need to start a session with:
<? session_start(); ?>then to store a value in the $_SESSION array just type:
$_SESSION['user_name']="Paul";When you want to access your session data from other page just:
1. Start the session with session_start().
Remember to place the above code at the top of your web page, otherwise you'll see errors!
2. To access the stored data just type:
echo $_SESSION['user_name'];That's it. Now you can watch the video for some more examples of sessions basic usage:
by Nevyan Neykov