Have you ever been amazed how the web works and how it handles accounts? For example everytime you log-in through Facebook, you will be asked for your username and password. After a successful log-in you will be directed through your own personal page with a corresponding link. However, when you copy and paste that exact link to a different computer you won't have access to it. Or take for example an Administrative account and you know the link to the control panel and stuff, but everytime you enter it directly you will be redirected to another page stating that you have no rights to access the page. We'll in this two part tutorial you are going to learn more about how Sessions work.
In PHP you can restrict someone from accessing to another page with the use of SUPERGLOBALS called $SESSIONS. Sessions from the name itself, store unique session of a user everytime a user opens-up his browser.
To start a session:
<?php session_start(); ?>
With the function session_start(); you are now starting a session. Always remember that if you are dealing with sessions you need to place the said function first and on every PHP page.
To store a session variable you can do so just like initializing an array.
<?php session_start(); $_SESSION['username'] = "john_doe24"; $_SESSION['usertype'] = "ADMIN"; ?>
From the example above, we have created stored the $SESSION variable with two keys namely username and usertype each with their own values "johndoe24" and "ADMIN" respectively.
So when you try to look-up what the contents of the $SESSION variable, you can do so with the vardump() function. In which it will display what keys and values are stored in the $_SESSION variable array.