This is NOT safe, this is just a tutorial.
Essentially, it's pretty easy if you got the right idea!
How to reach ...
First, when the user logs in successfully, create $ _SESSION [] var. $ _SESSION [] vars are used on almost every website that runs on PHP, you can start a simple session by adding the code below at the top of the page.
session_start();
Secondly, when your code says that the user login has been successfully created, the var session, as shown below, is the code.
$_SESSION['loggedIn'] = true;
Thirdly, you need to create some kind of code that checks if the user is logged in, a code example is shown below.
if (isset($_SESSION['loggedIn'])) { // code to execute if the user is logged in } else { // code to execute if the user is not logged in }
And finally, when you log out, you need to destroy the session and delete all $ _SESSION [] vars using the following code.
$_SESSION = array(); session_destroy();
Execute HTML based on if statement:
<?php if (isset($_SESSION['loggedIn'])) { ?> <p>Logged In!</p> <?php } else { ?> <p>Logged Out!</p> <?php } ?>
If you want to display the username, you need to create the username $ _SESSION [] var and use the echo file $ _SESSION [] var.
// To Echo <?= $_SESSION['username']; ?> // To Create <?php $_SESSION['username'] = 'someusername'; ?>
Here are some resources that can help you in security, I just looked at your code and is simple, but not very secure.
http://php.net/manual/en/session.security.php
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
source share