Inlogscript, voldoende OOP?
Ik heb een login script gemaakt maar nu ben ik benieuwd of ik nu goed OOP gebruik.
Alvast bedankt
Code (php)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* UserController
*/
class userController
{
/**
* @var Object
*/
private $database;
/**
* @var Object
*/
private $errors;
/**
* Constructor
*
* @param MySQL $database
*/
public function __construct( MySQL $database, ErrorList $errors )
{
$this->database = $database;
$this->errors = $errors;
}
/**
* Authenticate
*
* @param String $username
* @param String $password
*/
public function authenticate( $username, $password )
{
if( $this->checkInput( $username, $password ) )
{
try
{
$this->database->connect( 'localhost', 'username', 'password', 'database' );
$aQuery =
"
SELECT
user_id
user_name,
user_password
FROM
users
WHERE
user_name = '" .$this->database->addslashes( $username ). "'
AND
user_password = '" .$this->safePassword( $password ). "'
";
if( $result = $this->database->query( $aQuery )->rows( ) == 1 )
{
$row = $result->fetch_assoc();
if( !$this->checkSession( $row['user_id'], $row['user_name'] ) )
{
$this->createSession( $row['user_id'], $row['user_name'] );
}
else
{
$this->errors->add( 'Je bent reeds ingelogd' );
}
}
else
{
$this->errors->add( 'Er zijn geen resultaten gevonden in onze database' );
}
}
catch( loginException $e )
{
$this->errors->add( $e );
}
}
}
/**
* checkUsername
*
* @param String $username
* @return Bool
*/
private function checkUsername( $username )
{
if( null === $username )
{
return false;
}
return true;
}
/**
* checkPassword
*
* @param String $password
* @return Bool
*/
private function checkPassword( $password )
{
if( null === $password )
{
return false;
}
return true;
}
/**
* safePassword
*
* @param String $password
* @return String
*/
private function safePassword( $password )
{
return sha1( $password, new Salt( ); );
}
/**
* checkSession
*
* @param Int $user_id
* @param String $user_name
* @return Bool
*/
private function checkSession( $user_id, $user_name )
{
return ( isset( $_SESSION['user_id'] ) && isset( $_SESSION['user_name'] ) );
}
/**
* createSession
*
* @param Int $user_id
* @param String $user_name
*/
private function createSession( $user_id, $user_name )
{
$_SESSION['user_id'] = ( int ) $user_id;
$_SESSION['user_name'] = ( string ) $user_name;
}
/**
* Redirect
*
* @param String $url
*/
public function redirect( $url )
{
header("location: " .$url. "");
}
}
/**
* Login Exceptions
*/
class LoginException extends Exception
{
}
?>
/**
* UserController
*/
class userController
{
/**
* @var Object
*/
private $database;
/**
* @var Object
*/
private $errors;
/**
* Constructor
*
* @param MySQL $database
*/
public function __construct( MySQL $database, ErrorList $errors )
{
$this->database = $database;
$this->errors = $errors;
}
/**
* Authenticate
*
* @param String $username
* @param String $password
*/
public function authenticate( $username, $password )
{
if( $this->checkInput( $username, $password ) )
{
try
{
$this->database->connect( 'localhost', 'username', 'password', 'database' );
$aQuery =
"
SELECT
user_id
user_name,
user_password
FROM
users
WHERE
user_name = '" .$this->database->addslashes( $username ). "'
AND
user_password = '" .$this->safePassword( $password ). "'
";
if( $result = $this->database->query( $aQuery )->rows( ) == 1 )
{
$row = $result->fetch_assoc();
if( !$this->checkSession( $row['user_id'], $row['user_name'] ) )
{
$this->createSession( $row['user_id'], $row['user_name'] );
}
else
{
$this->errors->add( 'Je bent reeds ingelogd' );
}
}
else
{
$this->errors->add( 'Er zijn geen resultaten gevonden in onze database' );
}
}
catch( loginException $e )
{
$this->errors->add( $e );
}
}
}
/**
* checkUsername
*
* @param String $username
* @return Bool
*/
private function checkUsername( $username )
{
if( null === $username )
{
return false;
}
return true;
}
/**
* checkPassword
*
* @param String $password
* @return Bool
*/
private function checkPassword( $password )
{
if( null === $password )
{
return false;
}
return true;
}
/**
* safePassword
*
* @param String $password
* @return String
*/
private function safePassword( $password )
{
return sha1( $password, new Salt( ); );
}
/**
* checkSession
*
* @param Int $user_id
* @param String $user_name
* @return Bool
*/
private function checkSession( $user_id, $user_name )
{
return ( isset( $_SESSION['user_id'] ) && isset( $_SESSION['user_name'] ) );
}
/**
* createSession
*
* @param Int $user_id
* @param String $user_name
*/
private function createSession( $user_id, $user_name )
{
$_SESSION['user_id'] = ( int ) $user_id;
$_SESSION['user_name'] = ( string ) $user_name;
}
/**
* Redirect
*
* @param String $url
*/
public function redirect( $url )
{
header("location: " .$url. "");
}
}
/**
* Login Exceptions
*/
class LoginException extends Exception
{
}
?>
Er zijn nog geen reacties op dit bericht.