PHP login script is used to provide verification for our web pages. script works after submiting the user login button.
Example
#index.php
<?php
session_start();
?>
<html lang = "en">
<head>
<title>Rahorji.in - Login example</title>
</head>
<body>
<div class = "container form-signin">
<?php
$msg = '';
if (isset($_POST['login']) && !empty($_POST['username']) && !empty($_POST['password'])) {
if ($_POST['username'] == 'admin' &&
$_POST['password'] == '123456') {
$_SESSION['valid'] = true;
$_SESSION['timeout'] = time();
$_SESSION['username'] = 'admin';
echo 'you are logged in';
} else {
$msg = 'Wrong username or password';
}
}
?>
</div> <!-- /container -->
<div class = "container">
<?php if (!isset($_POST['username'])): ?>
<h2>Login to..</h2>
<form class = "form-signin" role = "form"
action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']);
?>" method = "post">
<h4 class = "form-signin-heading"><?php echo $msg; ?></h4>
<input type = "text" class = "form-control" placeholder="Username" name = "username" required autofocus></br></br>
<input type = "password" class = "form-control" name = "password" placeholder = "Password" required></br></br>
<button class = "btn btn-lg btn-primary btn-block" type = "submit" name = "login">Login</button>
</form>
<?php else: ?>
<a href = "logout.php" tite = "Logout">Logout<?php endif; ?>
</div>
</body>
</html>
The login page is based on the session. When a user closes a session, it will clear the session data and redirect to login page.
#logout.php
<?php
session_start();
unset($_SESSION["username"]);
unset($_SESSION["password"]);
echo 'You have cleaned session';
header('Refresh: 2; URL = index.php');
?>