login.php

Gesponsorde koppelingen

PHP script bestanden

  1. index.php
  2. class.user.php
  3. login.php
  4. phphulp.sql
  5. class.userMapper.php
  6. class.bruteForce.php
  7. class.bruteForceMapper.php
  8. class.database.php
  9. class.dataMapper.php
  10. class.hash.php

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php session_start();

    /**
     *
     * Account information:
     *
     *    $user = new User();
     *    $user->setUsername('Demo');
     *    $user->setPassword('Demo123');
     *
     */

    /**
     * On the submit press button, validate the input when the user isn't logged
     * in:
     */

    if($_SERVER['REQUEST_METHOD'] == 'POST' && !isset($_SESSION['user'])) {
    
        /**
         * Include all the classes that are required:
         */

        foreach(array('class.database', 'class.dataMapper', 'class.hash', 'class.user', 'class.userMapper', 'class.bruteForce', 'class.bruteForceMapper') as $file) {
            include 'classes/'.$file.'.php';
        }

        
        /**
         * Debug for the database class on or off:
         */

        define('DEBUG', true);
        
        /**
         * Set the database information, normally you should do that in a config
         * file:
         */

        $database        = new Database();
        $database->setHost('localhost');
        $database->setUsername('root');
        $database->setPassword('root');
        $database->setDatabase('phphulp');
        
        /**
         * Get the connection. When you use a config file, you do not need to call this
         * if you do not change from database information:
         */

        $database->getConnection();
    
        /**
         * Array to store errors, you could also use an own error class:
         */

        $errors            = array();
        
        /**
         * Set the user information:
         */

        $user            = new User();
        $user->setUsername(isset($_POST['username']) ? $_POST['username'] : '');
        $user->setPassword(isset($_POST['password']) ? $_POST['password'] : '');
        
        /**
         * When the username hasn't been submitted or isn't valid. A username can only
         * contain a-z, A-Z and 0-9 tokens:
         */

        if((isset($_POST['username']) && !preg_match('/^[0-9\p{L}]{2,12}$/u', $_POST['username'])) || !isset($_POST['username'])) {
            $errors[]    = 'The login credentials are not correct.';
        }

        
        else {
        
            /**
             * Create bruteforce object:
             */

            $bruteforce = new BruteForce();
            $bruteforce->setUsername($user->getUsername());
            $bruteforce->setIp($_SERVER['REMOTE_ADDR']);
            $bruteforce->setMaxAttempts(5);                                    /** Number of attempts, normally a value between 3 and 5 **/
            $bruteforce->setTimeout(5);                                        /** Timeout, normally a value between 5 and 15 **/
            
            /**
             * The new user information:
             */

            $user_db        = UserMapper::create()->getByLoginCredentials($user, 'user_id, username');
            
            /**
             * Check if there is a brute force ban for the user:
             */

            $bruteforce_mapper = new BruteForceMapper();
            if($bruteforce_mapper->getByUsername($bruteforce, 'brute_force_id') != false) {
                $errors[]    = 'You have a login ban. Please wait for max. 15 minutes.';
            }

            
            elseif($bruteforce_mapper->getByIp($bruteforce, 'brute_force_id') != false) {
                $errors[]    = 'You have a login ban. Please wait for max. 15 minutes.';
            }

        
            /**
             * Check if the user exists:
             */

            elseif($user_db == false) {
                $bruteforce_mapper->deleteByTime($bruteforce);        /** Delete the old bans, you could use this one in a cronjob **/
                $bruteforce_mapper->insert($bruteforce);            /** Insert the new one **/
                $errors[]    = 'The login credentials are not correct.';
            }

            
            /**
             * Validation of the user has been completed:
             */

            else {
                $_SESSION['user']['user_id']    = $user_db->user_id;
                $_SESSION['user']['username']    = $user_db->username;
                /** Add sessions and stuff **/
            }
        
        }
        
    }

    
?>

<!DOCTYPE html>
<html>
<head>
    
    <title>PHP Login With Brute Force Protection</title>
    
</head>
<body>

    <?php if(!isset($_SESSION['user'])): ?>
        <?php if(isset($errors) && is_array($errors) && count($errors) > 0): ?>
            <ul>
            <?php foreach($errors as $error): ?>
                <li><?php echo $error; ?></li>
            <?php endforeach; ?>
        <?php endif; ?>
        
        <form method="POST">
            <label for="username">Username (Demo):</label>
            <input type="text" name="username" value="" />
            <label for="password">Password (Demo123):</label>
            <input type="password" name="password" value="" />
            <input type="submit" value="Login" />
        </form>
    <?php endif; ?>
    
    <?php if(isset($_SESSION['user'])): ?>
        <?php echo 'Logged in. Welcome '.$_SESSION['user']['username'].'.'; ?>
    <?php endif; ?>
    
</body>
</html>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.