<?php
session_start();
$user = "admin"; // потребителско име
$pass = "12131415"; // парола
if (isSet($_SESSION['username'])) {
echo "Welcome {$user}!";
} else {
if (isSet($_POST['submit'])) {
if(isSet($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) {
if(isSet($_POST['username'], $_POST['password'])) {
$username = trim(htmlspecialchars($_POST['username']));
$password = trim(htmlspecialchars($_POST['password']));
} else {
$username = "";
$password = "";
}
if(!$username || !$password) {
echo "Username/password combination incorrect.";
} else {
if ($username == $user || $password == $pass) {
$_SESSION['username'] = $username;
echo "back to <a href=\"index.php\">home</a>";
} else {
echo "Invalid user/pass";
}
}
} else {
echo "CSRF Attack Detected";
}
} else {
$token = $_SESSION['token'] = md5(uniqid('shalqlqlq',true));
$GET_SELF = htmlentities($_SERVER['PHP_SELF']);
echo "
<form method=\"POST\" action=\"{$GET_SELF}\">
Username: <input type=\"text\" name=\"username\"><br />
Password: <input type=\"password\" name=\"password\"><br />
<input type=\"submit\" name=\"submit\" value=\"Login\">
<input type=\"hidden\" name=\"token\" value=\"{$token}\" />
</form>";
}
}
?>