Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php include 'includes/header.php'; include "functions.php";
- session_start();
- connectToDatabase();
- $email = '';
- if ($_SERVER['REQUEST_METHOD']=='POST') {
- list($errors, $input) = validate_form();
- if ($errors) {
- show_form($errors);
- } else {
- process_form($input);
- }
- } else {
- show_form();
- }
- function show_form($errors=array()){
- if ($errors) {
- $errorHtml = '<ul><li>';
- $errorHtml .= implode('</li><li>',$errors);
- $errorHtml .= '</li></ul>';
- } else {
- $errorHtml = '';
- }
- echo <<<_FORM_
- <form name = "login" method = "post" action = "$_SERVER[PHP_SELF]">$errorHtml
- <label for="e-mail">E-mail</label>
- <input type="email" id="e-mail" required autofocus>
- <label for="password">Password</label>
- <input type="password" id="password" required>
- <p><a href="register.php">Register</a> - <a href="#">Forgot Password</a> - <a href="#">Forgot Username</a></p>
- <div class="login-button">
- <input type="submit" value="Login" name="login_submit">
- </div>
- </form>
- _FORM_;
- }
- function validate_form() {
- $input = array();
- $errors = array();
- //Een aantal voorbeeldgebruikers
- $users = array('kut@kut.nl' => '123',
- 'bob' => 'mijnww',
- 'tom' => '**fun**');
- //Controleer gebruikersnaam
- $input['email'] = $_POST['e-mail'] ?? '';
- if (! array_key_exists($input['email'], $users)) {
- $errors[] = 'Please fill in a correct email';
- } else {
- //Controleer wachtwoord
- $saved_password = $users[$input['password']];
- $submitted_password = $_POST['password'] ?? '';
- if ($saved_password != $submitted_password) {
- $errors[] = 'Please fill in a correct password';
- }
- }
- return array($errors, $input);
- }
- function process_form($input) {
- //Voeg de gebruiker aan de sessie toe.
- $_SESSION['email'] = $input['email'];
- echo "Welkom, ".$_SESSION['email'];
- }
- include 'includes/footer.php'; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement