Index.php - VirajKanse/Student_Authentication GitHub Wiki
Again all this does is check if the user has filled the form correctly, verifies that their credentials match a record from the database and logs them in if it does.
After logging in, the user is redirected them to the "index.php" file with a success message.
So now, Open "Index.php" and paste the following code in it:
<?php
session_start();
if (!isset($_SESSION['rl'])) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['rl']);
header("location: login.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" type="text/css" href="hover.css">
<link rel="stylesheet" type="text/css" href="animate.css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" media="all">
</head>
<body>
<div class="header">
<h2 class="animated flip">Home Page</h2>
</div>
<div class="content">
<!-- notification message -->
<?php if (isset($_SESSION['success'])) : ?>
<div class="error success" >
<h3>
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</h3>
</div>
<?php endif ?>
<!-- logged in user information -->
<?php if (isset($_SESSION['rl'])) : ?>
<p>Welcome <strong class="hvr-bounce-in"><?php echo "Student ⊂((・▽・))⊃"; ?></strong></p>
<p> <a href="index.php?logout='1'" class="hvr-icon-hang">logout</a> </p>
<?php endif ?>
</div>
<p class="dev">@VirajKanse(ADevDX)</p>
</body>
</html>
The first if statement checks if the user is** already logged in**. If they are not logged in, they will be redirected to the login page. Hence this page is accessible to only logged in users. If you'd like to make any page accessible only to logged in users, all you have to do is place this if statement at the top of the file.
The second if statement checks if the user has clicked the logout button. If yes, the system logs them out and redirects them back to the login page.
And that's it!