Guest User

Untitled

a guest
Jul 28th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.42 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. // Connect to the database
  5. try {
  6.   $dbname = $user = $pass = 'admin';
  7.   $dbconn = new PDO('mysql:host=localhost;dbname='.$dbname, $user, $pass);
  8. }
  9. catch (Exception $e) {
  10.   echo "Error: " . $e->getMessage();
  11. }
  12.  
  13. //SQL Query I used
  14. /*
  15. INSERT INTO table_name VALUES (uid, 'herp', 'derp', sha1(salt + ':' + $pass))
  16. */
  17.  
  18. // Check login
  19. if (isset($_POST['login']) && $_POST['login'] == 'Login') {
  20.    echo "<pre>Trying to login as <b>$username</b></pre><br/>";
  21.   $salt_stmt = $dbconn->prepare('SELECT salt FROM users WHERE username=:username');
  22.   $salt_stmt->execute(array(':username' => $_POST['username']));
  23.   $res = $salt_stmt->fetch();
  24.   $salt = ($res) ? $res['salt'] : '';
  25.   echo "<pre>Got salt '<b>$salt</b>' from table.</pre><br/>";
  26.   echo "<pre>String to SHA: '<b>$salt{$_POST['pass']}</b>'.</pre><br/>";
  27.   $salted = sha1($salt . $_POST['pass']);
  28.   echo "<pre>SHA'ed string: '<b>$salted</b>'.</pre><br/>";
  29.  
  30.   $login_stmt = $dbconn->prepare('SELECT username, uid FROM users WHERE username=:username AND pass=:pass');
  31.   $login_stmt->execute(array(':username' => $_POST['username'], ':pass' => $salted));
  32.  
  33.  
  34.   if ($user = $login_stmt->fetch()) {
  35.     $_SESSION['username'] = $user['username'];
  36.     $_SESSION['uid'] = $user['uid'];
  37.   }
  38.   else {
  39.     $err = 'Incorrect username or password.';
  40.   }
  41. }
  42.  
  43. // Logout
  44. if (isset($_SESSION['username']) && isset($_POST['logout']) && $_POST['logout'] == 'Logout') {
  45.   // Unset the keys from the superglobal
  46.   unset($_SESSION['username']);
  47.   unset($_SESSION['uid']);
  48.   // Destroy the session cookie for this session
  49.   setcookie(session_name(), '', time() - 72000);
  50.   // Destroy the session data store
  51.   session_destroy();
  52.   $err = 'You have been logged out.';
  53. }
  54.  
  55.  
  56. ?>
  57. <!doctype html>
  58. <html>
  59. <head>
  60.   <title>Login</title>
  61. </head>
  62. <body>
  63.   <?php if (isset($_SESSION['username'])): ?>
  64.   <h1>Welcome, <?php echo htmlentities($_SESSION['username']) ?></h1>
  65.   <form method="post" action="login.php">
  66.     <input name="logout" type="submit" value="Logout" />
  67.   </form>
  68.   <?php else: ?>
  69.   <h1>Login</h1>
  70.   <?php if (isset($err)) echo "<p>$err</p>" ?>
  71.   <form method="post" action="login.php">
  72.     <label for="username">Username: </label><input type="text" name="username" />
  73.     <label for="pass">Password: </label><input type="password" name="pass" />
  74.     <input name="login" type="submit" value="Login" />
  75.   </form>
  76.   <?php endif; ?>
  77. </body>
  78. </html>
Add Comment
Please, Sign In to add comment