Guest User

Untitled

a guest
Apr 23rd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.87 KB | None | 0 0
  1. <?php
  2.  
  3. session_start();
  4.  
  5. define('GUESTBOOK_LOGIN_FILE', './.htpasswd');
  6.  
  7. $error = '';
  8.  
  9. if (isset($_POST['username']) && isset($_POST['password'])) {
  10.     $username = str_replace(array('.', ':', "\n", "\r"), '', $_POST['username']);
  11.     $password = trim($_POST['password']);
  12.  
  13.     $credentials = file(GUESTBOOK_LOGIN_FILE);
  14.  
  15.     $loggedIn = false;
  16.  
  17.     print $username . ' ' . $password;
  18.  
  19.     foreach($credentials as $c) {
  20.         print $c . "<br />";
  21.  
  22.         $c = explode(':', $c);
  23.         print crypt($password, $c[1]);
  24.         if(trim($c[0]) === $username && trim($c[1]) === crypt($password, $c[1])) {
  25.             $loggedIn = true;
  26.             break;
  27.         }
  28.     }
  29.  
  30.     if($loggedIn === false) {
  31.         $error = 'Invalid username or password';
  32.     } else {
  33.         $_SESSION['user'] = $username;
  34.         $_SESSION['logged_in'] = true;
  35.     }
  36. }
  37.  
  38. if($_SESSION['logged_in'] === TRUE) {
  39.     header('Location: guestbook.php');
  40.     exit;
  41. }
  42.  
  43. if(strlen(trim($error)) > 0)
  44.     $error = '<div class="error">' . $error . '</div>';
  45.  
  46. ?>
  47. <html>
  48.     <head>
  49.         <title>Guestbook</title>
  50.         <style>
  51.             body {
  52.                 width: 400px;
  53.                 margin: 20px auto;
  54.                 font-family: Helvetica, arial, serif;
  55.             }
  56.  
  57.             .signature {
  58.                 margin: 20px 0;
  59.             }
  60.  
  61.             .signature .name {
  62.                 font-size: 70%;
  63.             }
  64.  
  65.             input, textarea {
  66.                 width: 400px;
  67.                 padding: 4px;
  68.             }
  69.  
  70.             textarea {
  71.                 height: 100px;
  72.                 resize: none;
  73.             }
  74.  
  75.             input {
  76.                 height: 40px;
  77.                 margin: 5px 0;
  78.             }
  79.  
  80.             .error {
  81.                 background: rgba(255, 0, 0, 0.5);
  82.                 padding: 10px;
  83.                 text-align: center;
  84.                 color: white;
  85.                 border: 2px solid red;
  86.             }
  87.         </style>
  88.     </head>
  89.     <body>
  90.         <h1>Guestbook Login</h1>
  91.         <?php echo $error; ?>
  92.         <form action="?login" method="POST">
  93.             <input type="text" name="username" placeholder="Username" /><br />
  94.             <input type="password" name="password" placeholder="Password" /><br />
  95.             <input type="submit" value="Log In" />
  96.         </form>
  97.     </body>
  98. </html>
Add Comment
Please, Sign In to add comment