undefined message
ik ben een cursus aan het volgen van php advanced lynda.com
nu heb ik login pagina waarvan ik steeds de melding krijg:
Notice: Undefined variable: message in /Users/gebruiker/Sites/photo_gallery/public/admin/login.php on line 48
de login.php pagina is
<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
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
<?php
require_once("../../includes/functions.php");
require_once("../../includes/session.php");
require_once("../../includes/database.php");
require_once("../../includes/user.php");
if($session->is_logged_in()) {
redirect_to("index.php");
}
// Remember to give your form's submit tag a name="submit" attribute!
if (isset($_POST['submit'])) { // Form has been submitted.
$username = trim($_POST['username']);
$password = trim($_POST['password']);
// Check database to see if username/password exist.
$found_user = User::authenticate($username, $password);
if ($found_user) {
$session->login($found_user);
redirect_to("index.php");
} else {
//username/password combo was not found in database
$message = "Username/password combination incorrect.";
}
} else { //Form has not been submitted.
$username ="";
$password ="";
}
?>
require_once("../../includes/functions.php");
require_once("../../includes/session.php");
require_once("../../includes/database.php");
require_once("../../includes/user.php");
if($session->is_logged_in()) {
redirect_to("index.php");
}
// Remember to give your form's submit tag a name="submit" attribute!
if (isset($_POST['submit'])) { // Form has been submitted.
$username = trim($_POST['username']);
$password = trim($_POST['password']);
// Check database to see if username/password exist.
$found_user = User::authenticate($username, $password);
if ($found_user) {
$session->login($found_user);
redirect_to("index.php");
} else {
//username/password combo was not found in database
$message = "Username/password combination incorrect.";
}
} else { //Form has not been submitted.
$username ="";
$password ="";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Photo Gallery</title>
<link rel="stylesheet" type="text/css" href="../stylesheets/main.css"
</head>
<body>
<div id="header">
<h1>Photo Gallery</h1>
</div>
<div id="main">
<h2>Staff Login</h2>
<form action="login.php" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" maxlength="30" value=" "/>
</td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" maxlength="30" value="" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="Login" />
</td>
</tr>
</table>
</form>
</div>
<div id="footer">Copyright , Kevin Skoglund</div>
</body>
</html>
</code>
in de aparte function.php heb ik de functie staan :
<code>
function output_message($message="") {
if(!empty($message)) {
return "<p class=\"message\">{$message}</p>";
} else {
return "";
}
}
</code>
Als ik juist ben is als deze geen message vindt dat deze standard $message = "" invult
nu versta ik niet waar dan de message Undefined variable ?
kan iemand me hiermee helpen ?
Toevoeging op 12/03/2016 14:32:30:
lijn 48 is
Echter deze $message bestaat blijkbaar niet.
$message wordt alleen in de else aangemaakt en niet in de if().
ik heb heb opgelost door:
<code>
// Check database to see if username/password exist.
$found_user = User::authenticate($username, $password);
if ($found_user) {
$session->login($found_user);
redirect_to("index.php");
} else {
//username/password combo was not found in database
$message = "Username/password combination incorrect.";
}
} else { //Form has not been submitted.
$message = "Please login: ";
$username ="";
$password ="";
}
</code>