authentication-class

Gesponsorde koppelingen

PHP script bestanden

  1. authentication-class

« Lees de omschrijving en reacties

Database Layout:
CREATE TABLE `gebruikers` (
`gebruikerid` int(6) NOT NULL auto_increment,
`gebruikersnaam` varchar(50) NOT NULL,
`voornaam` varchar(40) NOT NULL,
`tussenvoegsel` varchar(25) NOT NULL,
`achternaam` varchar(40) NOT NULL,
`wachtwoord` varchar(40) NOT NULL,
`niveau` int(6) NOT NULL,

PRIMARY KEY (`gebruikerid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;


Database Connect('config.php')

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
<?php
session_start();

//Userlevels:

define('BANNED', 0);
define('QUEST', 1);
define('USER', 2);
define('VIP', 3);
define('REDACTEUR', 4);
define('MODERATOR', 5);
define('SUPERMODERATOR', 6);
define('ADMIN', 7);

include('clsAuthentication.php');
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
try
{
    $Auth = new Authentication($db);
}

catch(Exception $e)
{

    echo 'Message: ' .$e->getMessage();
}

?>



Voorbeeld('index.php');
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<?php
include('config.php');
$Auth->setRequiredAuth(QUEST);
$Auth->login('Gebruikersnaam', 'Wachtwoord');
?>



Classe ('clsAuthentication.php')
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
<?php
class Authentication
{
    static $db;
    private $m_sSaltkey;
    protected $m_aUserdata = array();
    
    function
__construct($p_oDatabaseConnection)
    {

        if(is_object($p_oDatabaseConnection))
        {

            $this->db = $p_oDatabaseConnection;
        }

        else
        {
            throw new Exception('Geen database Connection ?');
            exit;
        }

        $this->getLoginStatus();
    }

    
    public function login($p_sUsername, $p_sPassword)
    {

        $p_sUsername = $this->db->quote($p_sUsername);
        $p_sPassword = $this->db->quote($p_sPassword);
        if(!isset($_SESSION['user_id']))
        {

            if($this->getUserdata($p_sUsername, $p_sPassword))
            {

                $_SESSION['login_time'] = time();
                $_SESSION['user_id'] = $this->m_aUserdata['gebruikerid'];
                $_SESSION['server_generated'] = true;
                $_SESSION['username'] = $this->m_aUserdata['gebruikersnaam'];
                $_SESSION['ip_adress'] = $_SERVER['REMOTE_ADDR'];
                $_SESSION['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
                $_SESSION['auth_lvl'] = $this->aUserdata['niveau'];
            }
        }

        else
        {
            throw new Exception('Je bent al ingelogd');
        }
    }

    private function validateSession()
    {

        if(($_SERVER['REMOTE_ADDR'] != $_SESSION['ip_adress']) or ($_SERVER['HTTP_USER_AGENT'] != $_SESSION['user_agent']) or empty($_SESSION['server_generated']))
        {

            $this->logout();
            throw new Exception('Sessie informatie corrupt');
            return false;
        }

        return true;
    }

    
    private function getLoginStatus()
    {

        if(isset($_SESSION['user_id']))
        {

            if($this->validateSession())
            {

                return true;
            }
        }

        return false;
    }
    
    
    function
setRequiredAuth($p_nRequired)
    {

        if($this->getLoginStatus())
        {

            if($_SESSION['auth_lvl'] < $p_nRequired)
            {

                echo 'Je hebt niet genoeg rechten om hier te mogen komen';
                exit;
            }
        }

        else
        {
            if($p_nRequired > 1)
            {

                echo 'Je moet ingelogd zijn en de benodigede rechten hebben om hier te mogen komen';
                exit;
            }
        }

        return true;
    }

    
    private function getSaltkey()
    {

        if(is_file("salt.key") and is_readable("salt.key"))
        {

            $filePointer =  fopen('salt.key', 'r');
            $this->m_sSaltkey = fread($filePointer, 40);
            fclose($filePointer);
        }

        else
        {
            $filePointer = fopen('salt.key', 'w+');
            $aRange = range('A', 'z');
            shuffle($aRange);
            $sSaltkey = sha1(implode('', $aRange));
            fwrite($filePointer, $sSaltkey);
            chmod('salt.key', 660);
            $this->getSaltkey(); //recall it self to load 'new'
        }
    }

    
    private function getUserdata($p_sUsername, $p_sPassword)
    {

        $aSql = $this->db->query("
            SELECT *
            FROM gebruikers
            WHERE LOWER(gebruikersnaam) = LOWER('"
.$p_sUsername."') AND wachtwoord = '".sha1(sha1($p_sPassword).$this->m_sSaltkey)."'
            LIMIT 1"
);
        if(!is_null($aSql))
        {

            $this->m_aUserdata = $aSql;
        }

        else
        {
            throw new Exception('Gebruikersnaam of Wachtwoord fout');
            return false;
        }

        return true;
    }
    
    
    public function logout()
    {

        if(isset($_SESSION['user_id']))
        {

            session_destroy();
            return true;        
        }

        throw new Exception('Je kunt niet uitloggen als je niet bent ingelogd');
        return false;
    }
}

?>

 
 

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.