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
- etc..
Session and cookies - differences
1. Sessions use cookies, to set their unique id PHPSESSID=0g4sdf4agh27cghef3dhgahfh7j1gheg1j6, with no other data set in the cookie. This way server knows how to identify your session from others. The other method of doing this is passing the session id to the page URL as a parameter.
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
Post a Comment