Example of Sessions in PHP
Php / / July 04, 2021
Example of using Sessions in PHP. In this example, we will see how to create sessions, and to be able to use them in other separate pages, until the session of the user, usually when you close the browser.
File: access.php
Code:session_start ();
$ _SESSION [access] = true;
echo "OK";
?>
File: verify.php
Code:session_start ();
if ($ _ SESSION [access] == true) {
echo "OK, you are allowed access";
}
else {
echo "Error, you don't have permission.";
}
?>
In this simple example that we have divided into two files, we see that in the access.php is the file that will create a session called access, and when opening the page it will create the session, with a true or true value and then it will show OK. And then we open verify and it will show us that we have access allowed, since we open access.php. If we have not opened access.php, the verify.php will give us an error, since we do not have the session created that creates access.php. Really simple to understand and easy to implement for user systems in which you do not want to use COOKIES and prefer to use Sessions for more security.