Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>PHP - Login</title>
  6. </head>
  7. <body>
  8. <?php
  9.  
  10. function isUserLogged() {
  11. if (isset($_SESSION['logged']) && $_SESSION['logged'] === 1)
  12. return true;
  13. return false;
  14. }
  15.  
  16. function getUser() {
  17. return [
  18. 'login' => 'admin',
  19. 'password' => '123',
  20. ];
  21. }
  22.  
  23. function login($login, $password) {
  24. $user = getUser();
  25.  
  26. if ($login === $user['login'] && $password === $user['password']) {
  27. $_SESSION['login'] = $user['login'];
  28. $_SESSION['password'] = $user['password'];
  29. $_SESSION['logged'] = 1;
  30. return true;
  31. } else {
  32. return false;
  33. }
  34. }
  35.  
  36. session_start();
  37.  
  38. if (isset($_POST['submit'])) {
  39. $data = $_POST;
  40.  
  41. // print_r($data); // DEBUG
  42.  
  43. if (!login($data['login'], $data['password'])) {
  44. echo "Login e/ou senha incorreta!";
  45. }
  46. }
  47.  
  48. ?>
  49.  
  50. <?php if (isUserLogged()) : ?>
  51. <h1>User Panel</h1>
  52. <p>
  53. Login: <strong><?= getUser()['login'] ?></strong>
  54. <br> Password: <strong><?= getUser()['password'] ?></strong>
  55. </p>
  56. <?php else : ?>
  57. <form action="#" method="post">
  58. <label for="login">Login:</label>
  59. <input type="text" name="login" id="login" required>
  60. <br>
  61. <label for="password">Password:</label>
  62. <input type="password" name="password" id="password" required>
  63. <br>
  64. <button type="submit" name="submit">Login</button>
  65. </form>
  66. <?php endif; ?>
  67. </body>
  68. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement