In this tutorial we are going to learn how SESSIONS work. In web development, sessions are important as this will store session variable about an information or change a setting of a single user. PHP makes it easy thanks to Session variables. It goes like this, every time you are working with an application and open it, and if you plan on making changes the computer knows who you are.
The problem is that web servers don't know who you are because of the HTTP address doesn't have a constant state. PHP solves this type of dilemma by allowing you to store your personal data through session ID's . Sessions work by creating a unique id for each store variables based on the stored cookie.
To start a session is easy and you can do it like this.
<?php session_start(); ?>
<!-- START HTML CODE HERE -->
The session start function must appear before thetag of your document. The code above will register the user's session in the server that lets you start saving an assign UID for that user's session.
Session's work the same as $POST and $GET variables. You can store a session variable by doing this.
<?php session_start(); $_SESSION['user'] = "ADMIN"; ?>
From the code above, we have created a $_SESSION variable named "user" that contains the string "ADMIN".
To retrieve the Session variable you can do so by doing this.
<?php session_start(); $user_access = $_SESSION['user']; ?>
In which the variable $useraccess returns "ADMIN". Sessions are useful in programs that restrict access to certain pages and you can 301 redirect them to other pages.
To destroy a session you can use the sessiondestroy() to completely destroy all sessions.
<?php session_destroy(); ?>