Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- define('GUESTBOOK_LOGIN_FILE', './.htpasswd');
- $error = '';
- if (isset($_POST['username']) && isset($_POST['password'])) {
- $username = str_replace(array('.', ':', "\n", "\r"), '', $_POST['username']);
- $password = trim($_POST['password']);
- $credentials = file(GUESTBOOK_LOGIN_FILE);
- $loggedIn = false;
- print $username . ' ' . $password;
- foreach($credentials as $c) {
- print $c . "<br />";
- $c = explode(':', $c);
- print crypt($password, $c[1]);
- if(trim($c[0]) === $username && trim($c[1]) === crypt($password, $c[1])) {
- $loggedIn = true;
- break;
- }
- }
- if($loggedIn === false) {
- $error = 'Invalid username or password';
- } else {
- $_SESSION['user'] = $username;
- $_SESSION['logged_in'] = true;
- }
- }
- if($_SESSION['logged_in'] === TRUE) {
- header('Location: guestbook.php');
- exit;
- }
- if(strlen(trim($error)) > 0)
- $error = '<div class="error">' . $error . '</div>';
- ?>
- <html>
- <head>
- <title>Guestbook</title>
- <style>
- body {
- width: 400px;
- margin: 20px auto;
- font-family: Helvetica, arial, serif;
- }
- .signature {
- margin: 20px 0;
- }
- .signature .name {
- font-size: 70%;
- }
- input, textarea {
- width: 400px;
- padding: 4px;
- }
- textarea {
- height: 100px;
- resize: none;
- }
- input {
- height: 40px;
- margin: 5px 0;
- }
- .error {
- background: rgba(255, 0, 0, 0.5);
- padding: 10px;
- text-align: center;
- color: white;
- border: 2px solid red;
- }
- </style>
- </head>
- <body>
- <h1>Guestbook Login</h1>
- <?php echo $error; ?>
- <form action="?login" method="POST">
- <input type="text" name="username" placeholder="Username" /><br />
- <input type="password" name="password" placeholder="Password" /><br />
- <input type="submit" value="Log In" />
- </form>
- </body>
- </html>
Add Comment
Please, Sign In to add comment