Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. if(isset($_COOKIE['username'])) {
  5. header('Location: account.php');
  6. }
  7. ?>
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <title>Sample</title>
  12. </head>
  13. <body>
  14. <?php if(isset($_SESSION['login_error'])): ?>
  15. <div class="error_message">Incorrect username and password combination</div>
  16. <?php endif; ?>
  17. <form action="login.php" method="post">
  18. <input type="text" name="username" placeholder="Username" autocomplete="off"><br>
  19. <input type="password" name="password" placeholder="Password"><br>
  20. <input type="submit" value="Log in">
  21. </form>
  22.  
  23. <?php
  24. session_start();
  25.  
  26. $db = new PDO('mysql:host=127.0.0.1;dbname=sample;charset=utf8', 'root', '');
  27.  
  28. if(isset($_POST['username'], $_POST['password'])) {
  29. if(!empty($_POST['username']) && !empty($_POST['password'])) {
  30. $query = $db->prepare("SELECT username, lastname FROM users WHERE username=:username AND password=:password");
  31. $query->execute([
  32. 'username' => $_POST['username'],
  33. 'password' => $_POST['password']
  34. ]);
  35.  
  36. $row = $query->fetchAll(PDO::FETCH_OBJ);
  37.  
  38. if(count($row)) {
  39. $_SESSION['username'] = $_POST['username'];
  40. setcookie('username', $_POST['username'], time()+3600);
  41. header('Location: account.php');
  42. } else {
  43. $_SESSION['login_error'] = 'Incorrect username and password combination';
  44. header('Location: index.php');
  45. }
  46. } else {
  47. header('Location: index.php');
  48. }
  49. }
  50.  
  51. <?php
  52. session_start();
  53.  
  54. if(!isset($_COOKIE['username'])) {
  55. header('Location: index.php');
  56. }
  57.  
  58. $db = new PDO('mysql:host=127.0.0.1;dbname=sample;charset=utf8', 'root', '');
  59. $query = $db->prepare("SELECT firstname, lastname FROM users WHERE username=:username");
  60.  
  61. $query->execute(['username' => $_SESSION['username']]);
  62.  
  63. $row = $query->fetchAll(PDO::FETCH_OBJ);
  64. ?>
  65. <!DOCTYPE html>
  66. <html>
  67. <head>
  68. <title>Account</title>
  69. </head>
  70. <body>
  71. <?php if(isset($_SESSION['username'])): ?>
  72. <div>Hello, <?php echo $row->firstname; ?></div>
  73. <?php endif; ?>
  74.  
  75. <a href="logout.php">Logout</a>
  76. </body>
  77. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement