- Apr 25, 2015
- 1,845
- 2
- 2,199
- 327
This is a very basic authentication script and I do not recommend using this in a production environment. Does anyone else have their own PHP login/authentication scripts? Feel free to share yours! Also, highlight security issues you notice or what could be improved.
PHP:
<?php
session_start();
$setPassword = "Cake";
//You may change the password, this is just a simple system for the foundation(s) of a VERY SMALL AUTH system.
if (isset($_GET['logout'])) {
session_destroy();
header('Location: password.php');
}
if (isset($_SESSION['welcome'])) {
if ($_SESSION['welcome'] == $setPassword) {
echo "You are authorized with current credentials.";
echo "<hr><a href='?logout'>Logout</a>";
die;
} else {
echo "Your credentials have become invalid. Your session was just destroyed.";
session_destroy();
}
}
?>
<div align="right">
Verify your identity:
<form action="password.php" method="post">
<input type="password" name="auth">
<input type="submit" value="Authorize">
</form>
<?php
if (isset($_POST["auth"])) {
if ($_SESSION['fails'] > 2) {
die("Invalid login attempt.");
}
$auth = $_POST["auth"];
if ($auth == $setPassword) {
unset($_SESSION['fails']);
$_SESSION['welcome'] = $setPassword;
echo '
<h1>If you were not redirected, reload the page.</h1>
<meta http-equiv="refresh" content="0">
';
} else {
$_SESSION['fails']++;
//echo $_SESSION['fails'];
//echo "Invalid attempt, you have ".$_SESSION['fails']."/3 failed attempts.";
echo "Invalid login attempt.";
}
}
?>
</div>