PHP: Errors op een andere plek zetten
Ik heb een inlogformulier waarbij inlog-errors boven het gebruikersnaamveld verschijnen, maar ik wil dat deze tussen het wachtwoordveld en de inlogknop verschijnen.
Doordat ik met session_start(); begin kan ik niet zomaar de bovenste PHP-code tussen het wachtwoordveld en de loginknop zetten (dan krijg ik namelijk allemaal PHP-errors).
Hoe zorg ik ervoor dat de errors tussen het wachtwoordveld en de inlogknop verschijnen? Zie hieronder de code:
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
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
<?php
session_start();
if(isset($_POST['login']))
{
include('class.login.php');
$user = new Login();
if($user->isLoggedIn())
header('location: index.php');
else
$user->showErrors(); //<-- hier verschijnen nu de errors
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<?php include("includes/standard.php"); ?>
</head>
<body>
<?php include("includes/header1.php"); ?>
<div data-role="content">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<fieldset>
<label for="number"><h4>Gebruikersnaam:</h4></label>
<input type="number" name="username" />
<br>
<label for="password"><h4>Wachtwoord:</h4></label>
<input type="password" name="password" />
<input type="hidden" name="token" value="<?php echo $token;?>" />
<br>
<?php hier moeten errors verschijnen ?>
<input type="submit" name="login" value="Log in" data-role="button" data-theme="b" />
</fieldset>
</form>
</div>
<?php include("includes/footer.php"); ?>
session_start();
if(isset($_POST['login']))
{
include('class.login.php');
$user = new Login();
if($user->isLoggedIn())
header('location: index.php');
else
$user->showErrors(); //<-- hier verschijnen nu de errors
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<?php include("includes/standard.php"); ?>
</head>
<body>
<?php include("includes/header1.php"); ?>
<div data-role="content">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<fieldset>
<label for="number"><h4>Gebruikersnaam:</h4></label>
<input type="number" name="username" />
<br>
<label for="password"><h4>Wachtwoord:</h4></label>
<input type="password" name="password" />
<input type="hidden" name="token" value="<?php echo $token;?>" />
<br>
<?php hier moeten errors verschijnen ?>
<input type="submit" name="login" value="Log in" data-role="button" data-theme="b" />
</fieldset>
</form>
</div>
<?php include("includes/footer.php"); ?>
Gewijzigd op 15/10/2012 17:11:11 door Tobias Boekwijt
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
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
<?php
session_start();
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<?php include("includes/standard.php"); ?>
</head>
<body>
<?php include("includes/header1.php"); ?>
<div data-role="content">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<fieldset>
<label for="number"><h4>Gebruikersnaam:</h4></label>
<input type="number" name="username" />
<br>
<label for="password"><h4>Wachtwoord:</h4></label>
<input type="password" name="password" />
<input type="hidden" name="token" value="<?php echo $token;?>" />
<br>
<?php
if(isset($_POST['login']))
{
include('class.login.php');
$user = new Login();
if($user->isLoggedIn())
header('location: index.php');
else
$user->showErrors(); //<-- hier verschijnen nu de errors
}
?>
<input type="submit" name="login" value="Log in" data-role="button" data-theme="b" />
</fieldset>
</form>
</div>
<?php include("includes/footer.php"); ?>
session_start();
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<?php include("includes/standard.php"); ?>
</head>
<body>
<?php include("includes/header1.php"); ?>
<div data-role="content">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<fieldset>
<label for="number"><h4>Gebruikersnaam:</h4></label>
<input type="number" name="username" />
<br>
<label for="password"><h4>Wachtwoord:</h4></label>
<input type="password" name="password" />
<input type="hidden" name="token" value="<?php echo $token;?>" />
<br>
<?php
if(isset($_POST['login']))
{
include('class.login.php');
$user = new Login();
if($user->isLoggedIn())
header('location: index.php');
else
$user->showErrors(); //<-- hier verschijnen nu de errors
}
?>
<input type="submit" name="login" value="Log in" data-role="button" data-theme="b" />
</fieldset>
</form>
</div>
<?php include("includes/footer.php"); ?>
1) Ik kan door het verplaatsen van de if(isset(..)) nu niet meer inloggen.
2) Als ik om te testen een niet bestaande combinatie van gebruikersnaam/wachtwoord invoer verschijnt de verkeerde error, namelijk "Deze pagina is verlopen" terwijl er eerst "Onjuiste gebruikersnaam/wachtwoord" verscheen.
Hoe zorg ik ervoor dat ik weer kan inloggen en de juiste error verschijnt?
Zie hieronder de code van class.login.php die wordt ge-include in het stuk PHP in de vorige post:
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
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
<?php
class Login
{
private $_id;
private $_username;
private $_password;
private $_passmd5;
private $_errors;
private $_access;
private $_user;
private $_token;
public function __construct()
{
$this->_errors = array();
$this->_user = isset($_POST['login'])? 1 : 0;
$this->_access = 0;
$this->_token = $_POST['token'];
$this->_id = 0;
$this->_username = ($this->_user)? $this->filter($_POST['username']) : $_SESSION['username'];
$this->_password = ($this->_user)? $this->filter($_POST['password']) : '';
$this->_passmd5 = ($this->_user)? md5($this->_password) : $_SESSION['password'];
}
public function isLoggedIn()
{
($this->_user)? $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('<h4 id="error">Deze pagina is verlopen</h4>');
if(!$this->isDataValid())
throw new Exception('<h4 id="error">Gebruikersnaam bevat letters/symbolen<br>of wachtwoord bevat symbolen</h4>');
if(!$this->verifyDatabase())
throw new Exception('<h4 id="error">Onjuiste gebruikersnaam/wachtwoord</h4>');
$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
$db = mysql_connect("localhost", "root", "root"); mysql_select_db("test", $db);
$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()
{
//Username is only numbers
return (preg_match('/^[0-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()
{
foreach($this->_errors as $key=>$value)
echo $value."<br>";
}
}
?>
class Login
{
private $_id;
private $_username;
private $_password;
private $_passmd5;
private $_errors;
private $_access;
private $_user;
private $_token;
public function __construct()
{
$this->_errors = array();
$this->_user = isset($_POST['login'])? 1 : 0;
$this->_access = 0;
$this->_token = $_POST['token'];
$this->_id = 0;
$this->_username = ($this->_user)? $this->filter($_POST['username']) : $_SESSION['username'];
$this->_password = ($this->_user)? $this->filter($_POST['password']) : '';
$this->_passmd5 = ($this->_user)? md5($this->_password) : $_SESSION['password'];
}
public function isLoggedIn()
{
($this->_user)? $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('<h4 id="error">Deze pagina is verlopen</h4>');
if(!$this->isDataValid())
throw new Exception('<h4 id="error">Gebruikersnaam bevat letters/symbolen<br>of wachtwoord bevat symbolen</h4>');
if(!$this->verifyDatabase())
throw new Exception('<h4 id="error">Onjuiste gebruikersnaam/wachtwoord</h4>');
$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
$db = mysql_connect("localhost", "root", "root"); mysql_select_db("test", $db);
$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()
{
//Username is only numbers
return (preg_match('/^[0-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()
{
foreach($this->_errors as $key=>$value)
echo $value."<br>";
}
}
?>
Gewijzigd op 15/10/2012 15:47:19 door Tobias Boekwijt
Weet iemand hoe ik deze twee bugs kan oplossen?
Opgelost! /topic