Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. <?php include 'includes/header.php'; include "functions.php";
  2. session_start();
  3. connectToDatabase();
  4. $email = '';
  5. if ($_SERVER['REQUEST_METHOD']=='POST') {
  6. list($errors, $input) = validate_form();
  7. if ($errors) {
  8. show_form($errors);
  9. } else {
  10. process_form($input);
  11. }
  12. } else {
  13. show_form();
  14. }
  15.  
  16. function show_form($errors=array()){
  17.  
  18. if ($errors) {
  19. $errorHtml = '<ul><li>';
  20. $errorHtml .= implode('</li><li>',$errors);
  21. $errorHtml .= '</li></ul>';
  22. } else {
  23. $errorHtml = '';
  24. }
  25. echo <<<_FORM_
  26. <form name = "login" method = "post" action = "$_SERVER[PHP_SELF]">$errorHtml
  27. <label for="e-mail">E-mail</label>
  28. <input type="email" id="e-mail" required autofocus>
  29. <label for="password">Password</label>
  30. <input type="password" id="password" required>
  31. <p><a href="register.php">Register</a> - <a href="#">Forgot Password</a> - <a href="#">Forgot Username</a></p>
  32. <div class="login-button">
  33. <input type="submit" value="Login" name="login_submit">
  34. </div>
  35. </form>
  36. _FORM_;
  37. }
  38.  
  39. function validate_form() {
  40. $input = array();
  41. $errors = array();
  42.  
  43. //Een aantal voorbeeldgebruikers
  44. $users = array('kut@kut.nl' => '123',
  45. 'bob' => 'mijnww',
  46. 'tom' => '**fun**');
  47. //Controleer gebruikersnaam
  48. $input['email'] = $_POST['e-mail'] ?? '';
  49. if (! array_key_exists($input['email'], $users)) {
  50. $errors[] = 'Please fill in a correct email';
  51. } else {
  52. //Controleer wachtwoord
  53. $saved_password = $users[$input['password']];
  54. $submitted_password = $_POST['password'] ?? '';
  55. if ($saved_password != $submitted_password) {
  56. $errors[] = 'Please fill in a correct password';
  57. }
  58. }
  59. return array($errors, $input);
  60. }
  61.  
  62. function process_form($input) {
  63. //Voeg de gebruiker aan de sessie toe.
  64. $_SESSION['email'] = $input['email'];
  65. echo "Welkom, ".$_SESSION['email'];
  66. }
  67. include 'includes/footer.php'; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement