Inlogsysteem
Albert de Wit op 27/08/2012 16:08:10:
misschien om mee te beginnen -> http://www.youtube.com/watch?v=4oSCuEtxRK8
Ik heb dit al eerder gepost en nog blijf je hangen voor een tutorial, hier heb je hem nogmaals.
Als de TS nou gewoon dat ene stukje relevante code post kunnen we hem helpen.
Dit is geen chinees heh...
class.login.php:
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
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
<?php
class Login
{
private $_id;
private $_username;
private $_password;
private $_passmd5;
private $_errors;
private $_access;
private $_login;
private $_token;
public function __construct()
{
$this->_errors = array();
$this->_login = isset($_POST['login'])? 1 : 0;
$this->_access = 0;
$this->_token = $_POST['token'];
$this->_id = 0;
$this->_username = ($this->_login)? $this->filter($_POST['username']) : $_SESSION['username'];
$this->_password = ($this->_login)? $this->filter($_POST['password']) : '';
$this->_passmd5 = ($this->_login)? md5($this->_password) : $_SESSION['password'];
}
public function isLoggedIn()
{
($this->_login)? $this->verifyPost() : $this->verifySession();
return $this->_access;
}
public function filter($var)
{
return preg_replace('/[^a-zA-Z0-9]/','',$var);
}
public function verifyPost()
{
try
{
if(!$this->isTokenValid())
throw new Exception('Invalid Form Submission');
if(!$this->isDataValid())
throw new Exception('Invalid Form Data');
if(!$this->verifyDatabase())
throw new Exception('Invalid Username/Password');
$this->_access = 1;
$this->registerSession();
}
catch(Exception $e)
{
$this->_errors[] = $e->getMessage();
}
}
public function verifySession()
{
if($this->sessionExist() && $this->verifyDatabase())
$this->_access = 1;
}
public function verifyDatabase()
{
//Database Connection Data
mysql_connect("localhost", "root", "********") or die(mysql_error());
mysql_select_db("UG") or die(mysql_error());
$data = mysql_query("SELECT ID FROM users WHERE username = '{$this->_username}' AND password = '{$this->_passmd5}'");
if(mysql_num_rows($data))
{
list($this->_id) = @array_values(mysql_fetch_assoc($data));
return true;
}
else
{ return false; }
}
public function isDataValid()
{
return (preg_match('/^[a-zA-Z0-9]{5,12}$/',$this->_username) && preg_match('/^[a-zA-Z0-9]{5,12}$/',$this->_password))? 1 : 0;
}
public function isTokenValid()
{
return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 0 : 1;
}
public function registerSession()
{
$_SESSION['ID'] = $this->_id;
$_SESSION['username'] = $this->_username;
$_SESSION['password'] = $this->_passmd5;
}
public function sessionExist()
{
return (isset($_SESSION['username']) && isset($_SESSION['password']))? 1 : 0;
}
public function showErrors()
{
echo "<h3>Errors</h3>";
foreach($this->_errors as $key=>$value)
echo $value."<br>";
}
}
?>
class Login
{
private $_id;
private $_username;
private $_password;
private $_passmd5;
private $_errors;
private $_access;
private $_login;
private $_token;
public function __construct()
{
$this->_errors = array();
$this->_login = isset($_POST['login'])? 1 : 0;
$this->_access = 0;
$this->_token = $_POST['token'];
$this->_id = 0;
$this->_username = ($this->_login)? $this->filter($_POST['username']) : $_SESSION['username'];
$this->_password = ($this->_login)? $this->filter($_POST['password']) : '';
$this->_passmd5 = ($this->_login)? md5($this->_password) : $_SESSION['password'];
}
public function isLoggedIn()
{
($this->_login)? $this->verifyPost() : $this->verifySession();
return $this->_access;
}
public function filter($var)
{
return preg_replace('/[^a-zA-Z0-9]/','',$var);
}
public function verifyPost()
{
try
{
if(!$this->isTokenValid())
throw new Exception('Invalid Form Submission');
if(!$this->isDataValid())
throw new Exception('Invalid Form Data');
if(!$this->verifyDatabase())
throw new Exception('Invalid Username/Password');
$this->_access = 1;
$this->registerSession();
}
catch(Exception $e)
{
$this->_errors[] = $e->getMessage();
}
}
public function verifySession()
{
if($this->sessionExist() && $this->verifyDatabase())
$this->_access = 1;
}
public function verifyDatabase()
{
//Database Connection Data
mysql_connect("localhost", "root", "********") or die(mysql_error());
mysql_select_db("UG") or die(mysql_error());
$data = mysql_query("SELECT ID FROM users WHERE username = '{$this->_username}' AND password = '{$this->_passmd5}'");
if(mysql_num_rows($data))
{
list($this->_id) = @array_values(mysql_fetch_assoc($data));
return true;
}
else
{ return false; }
}
public function isDataValid()
{
return (preg_match('/^[a-zA-Z0-9]{5,12}$/',$this->_username) && preg_match('/^[a-zA-Z0-9]{5,12}$/',$this->_password))? 1 : 0;
}
public function isTokenValid()
{
return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 0 : 1;
}
public function registerSession()
{
$_SESSION['ID'] = $this->_id;
$_SESSION['username'] = $this->_username;
$_SESSION['password'] = $this->_passmd5;
}
public function sessionExist()
{
return (isset($_SESSION['username']) && isset($_SESSION['password']))? 1 : 0;
}
public function showErrors()
{
echo "<h3>Errors</h3>";
foreach($this->_errors as $key=>$value)
echo $value."<br>";
}
}
?>
Login.php
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
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
<?php
include "Thema.php"
?>
<h3><?php
session_start();
if(isset($_POST['login']))
{
include "class.login.php";
$login = new Login();
if($login->isLoggedIn())
header('location: index2.php');
else
$login->showErrors();
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table>
<tr><td><h4>Gebruikersnaam:</h4></td><td><input type="text" name="username" /></td></tr>
<tr><td><h4>Wachtwoord:</h4></td><td><input type="password" name="password" /></td></tr>
</table>
<input type="hidden" name="token" value="<?php echo $token;?>" />
<input type="submit" name="login" value="Login" />
</form></h3>
include "Thema.php"
?>
<h3><?php
session_start();
if(isset($_POST['login']))
{
include "class.login.php";
$login = new Login();
if($login->isLoggedIn())
header('location: index2.php');
else
$login->showErrors();
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table>
<tr><td><h4>Gebruikersnaam:</h4></td><td><input type="text" name="username" /></td></tr>
<tr><td><h4>Wachtwoord:</h4></td><td><input type="password" name="password" /></td></tr>
</table>
<input type="hidden" name="token" value="<?php echo $token;?>" />
<input type="submit" name="login" value="Login" />
</form></h3>
class.register.php
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
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
<?php
class Register
{
private $username;
private $password;
private $passmd5;
private $email;
private $errors;
private $token;
public function __construct()
{
$this->errors = array();
$this->username = $this->filter($_POST['ruser']);
$this->password = $this->filter($_POST['rpass']);
$this->email = $this->filter($_POST['remail']);
$this->token = $_POST['token'];
$this->passmd5 = md5($this->password);
}
public function process()
{
if($this->valid_token() && $this->valid_data())
$this->register();
return count($this->errors)? 0 : 1;
}
public function filter($var)
{
return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
}
public function register()
{
mysql_connect("localhost", "root", "*******") or die(mysql_error());
mysql_select_db("UG") or die(mysql_error());
mysql_query("INSERT INTO users(username,password) VALUES ('{$this->username}','{$this->passmd5}')");
if(mysql_affected_rows()< 1)
$this->errors[] = 'Could Not Process Form';
}
public function user_exists()
{
mysql_connect("localhost", "root", "********") or die(mysql_error());
mysql_select_db("UG") or die(mysql_error());
$data = mysql_query("SELECT ID FROM users WHERE username = '{$this->username}'");
return mysql_num_rows($data)? 1 : 0;
}
public function show_errors()
{
echo "<h3>Errors</h3>";
foreach($this->errors as $key=>$value)
echo $value."<br>";
}
public function valid_data()
{
if($this->user_exists())
$this->errors[] = 'Gebruikersnaam is al ingebruik';
if(empty($this->username))
$this->errors[] = 'Verkeerde gebruikersnaam';
if(empty($this->password))
$this->errors[] = 'Verkeerd wachtwoord';
if(empty($this->email) || !eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$',$this->email))
$this->errors[] = 'Geen geldig Email';
return count($this->errors)? 0 : 1;
}
public function valid_token()
{
if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
$this->errors[] = 'Error probeer later nog is';
return count($this->errors)? 0 : 1;
}
}
?>
class Register
{
private $username;
private $password;
private $passmd5;
private $email;
private $errors;
private $token;
public function __construct()
{
$this->errors = array();
$this->username = $this->filter($_POST['ruser']);
$this->password = $this->filter($_POST['rpass']);
$this->email = $this->filter($_POST['remail']);
$this->token = $_POST['token'];
$this->passmd5 = md5($this->password);
}
public function process()
{
if($this->valid_token() && $this->valid_data())
$this->register();
return count($this->errors)? 0 : 1;
}
public function filter($var)
{
return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
}
public function register()
{
mysql_connect("localhost", "root", "*******") or die(mysql_error());
mysql_select_db("UG") or die(mysql_error());
mysql_query("INSERT INTO users(username,password) VALUES ('{$this->username}','{$this->passmd5}')");
if(mysql_affected_rows()< 1)
$this->errors[] = 'Could Not Process Form';
}
public function user_exists()
{
mysql_connect("localhost", "root", "********") or die(mysql_error());
mysql_select_db("UG") or die(mysql_error());
$data = mysql_query("SELECT ID FROM users WHERE username = '{$this->username}'");
return mysql_num_rows($data)? 1 : 0;
}
public function show_errors()
{
echo "<h3>Errors</h3>";
foreach($this->errors as $key=>$value)
echo $value."<br>";
}
public function valid_data()
{
if($this->user_exists())
$this->errors[] = 'Gebruikersnaam is al ingebruik';
if(empty($this->username))
$this->errors[] = 'Verkeerde gebruikersnaam';
if(empty($this->password))
$this->errors[] = 'Verkeerd wachtwoord';
if(empty($this->email) || !eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$',$this->email))
$this->errors[] = 'Geen geldig Email';
return count($this->errors)? 0 : 1;
}
public function valid_token()
{
if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
$this->errors[] = 'Error probeer later nog is';
return count($this->errors)? 0 : 1;
}
}
?>
register.php
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
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
<?php
include "Thema.php";
?>
<h3>
<?php
session_start();
if(isset($_POST['register']))
{
include_once "class.register.php";
$register = new Register();
if($register->process())
echo "<h6>Successvol geregistreert!</h6>";
else
$register->show_errors();
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table>
<tr><td><h4>Gebruikersnaam:</h4></td><td><input type="text" name="ruser"/></td></tr>
<tr><td><h4>Wachtwoord:</h4></td><td><input type="password" name="rpass"/></td></tr>
<tr><td><h4>E-mail:</h4></td><td><input type="text" name="remail"/></td></tr>
</table>
<input type="hidden" name="token" value="<?php echo $token;?>"/>
<input type="submit" name="register" value="Registreer"/>
</form>
</h3>
include "Thema.php";
?>
<h3>
<?php
session_start();
if(isset($_POST['register']))
{
include_once "class.register.php";
$register = new Register();
if($register->process())
echo "<h6>Successvol geregistreert!</h6>";
else
$register->show_errors();
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table>
<tr><td><h4>Gebruikersnaam:</h4></td><td><input type="text" name="ruser"/></td></tr>
<tr><td><h4>Wachtwoord:</h4></td><td><input type="password" name="rpass"/></td></tr>
<tr><td><h4>E-mail:</h4></td><td><input type="text" name="remail"/></td></tr>
</table>
<input type="hidden" name="token" value="<?php echo $token;?>"/>
<input type="submit" name="register" value="Registreer"/>
</form>
</h3>
zo dat was het
Ik ga geen 200 regels doorspitten op zoek naar iets wat jij wellicht zelf ook kan zien....
Wat wilde je ook al weer? Jezelf admin maken?
Gewijzigd op 30/08/2012 20:24:47 door Eddy E
Eddy Erkelens op 30/08/2012 20:23:03:
En wat is de relevante code nu?
Ik ga geen 200 regels doorspitten op zoek naar iets wat jij wellicht zelf ook kan zien....
Wat wilde je ook al weer? Jezelf admin maken?
Ik ga geen 200 regels doorspitten op zoek naar iets wat jij wellicht zelf ook kan zien....
Wat wilde je ook al weer? Jezelf admin maken?
Ja en dat als je ingelogt bent alleen de button adminvoor jou zigtbaar is en dat de admin pagina checkt of je ook egt admin bent
Kan je je admin keuze niet in je database zetten? Altijd handig voor als je er later een admin bij wil, of hulpadmin/moderator...
http://www.dreamincode.net/forums/topic/290181-login-class-error/
aka
ik denk niet dat je dit zelf hebt gemaakt. even een tip hoor... antwoorden krijgen en kopieeren maakt je geen php programmeur, dat moet je zelf leren, ik heb al eerder een link gegeven hoe je simpel kan beginnen. succes
Gewijzigd op 31/08/2012 11:09:13 door Albert de Wit
als je dit als OOP wilt gaan doen zou je sowieso een PERSON class o.i.d. moeten hebben. waar de algemene dingen in komen en vervolgens iets van ADMIN class en een USER class die allebei de PERSON class extenden beide met hun eigen rechten etc.. probeer het is eerst met normale php en zonder kopieren plakken. eventueel de basis van php leren en dan beginnen met simpele projectjes ...
totaal geen kennis van php maar ook niet de moeite doen om het te leren
Als je toch iets wilt maken als registreren in OOP, maak je beter een UserMapper en daarin een create() functie ofzo.
Waarom verbind je trouwens bij iedere functie met MySQL?
Gewijzigd op 31/08/2012 15:35:17 door - Raoul -
hij verbond ze niet want dit is niet zijn code, hij heeft dit ergens vandaan gekopieerd. als dit zijn code was, hoefde hij onze hulp niet