eldan88 Posted October 13, 2013 Share Posted October 13, 2013 Hey. I am trying to create a login page but every time i preview the page it keeps saying me the following message: "Notice: Undefined variable: message in /media/sf_sandbox/photo_gallery/public/admin/login.php on line 45". I have a function called output_message that tells the $message to out put only when its not empty. I don't understand why it is giving me that notice. Below is the code for the login page. I have add the comment // Giving me an error message? on the line number that showing the error I have also included the output_message function Thanks for your help! <?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"); } 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 the database $message = "Username/password combination incorrect."; } } else { // Form has not been submitted. $username = ""; $password = ""; } ?> <html> <head> <title>Photo Gallery</title> <link href="../stylesheets/main.css" media="all" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1>Photo Gallery</h1> </div> <div id="main"> <h2>Staff Login</h2> <?php echo output_message($message);// Giving me an error message? ?> <form action="login.php" method="post"> <table> <tr> <td>Username:</td> <td> <input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" /> </td> </tr> <tr> <td>Password:</td> <td> <input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" /> </td> </tr> <tr> <td colspan="2"> <input type="submit" name="submit" value="Login" /> </td> </tr> </table> </form> </div> <div id="footer">Copyright <?php echo date("Y", time()); ?></div> </body> </html> <?php if(isset($database)) { $database->close_connection(); } ?> Output function function output_message($message="") { if (!empty($message)) { return "<p class=\"message\">{$message}</p>"; } else { return ""; } } Link to comment https://forums.phpfreaks.com/topic/282934-need-help-with-a-login-page/ Share on other sites More sharing options...
Ch0cu3r Posted October 13, 2013 Share Posted October 13, 2013 You're getting the error because the variable $message doesn't exist when $found_user returns true. When $found_user returns false $message will be set. if ($found_user) { $session->login($found_user); redirect_to("index.php"); } else { // username/password combo was not found in the database $message = "Username/password combination incorrect."; // <-- this is only set when $found_user is false } You need to check to see if $message exists before calling output_message function. Link to comment https://forums.phpfreaks.com/topic/282934-need-help-with-a-login-page/#findComment-1453778 Share on other sites More sharing options...
Ch0cu3r Posted October 13, 2013 Share Posted October 13, 2013 You need to check to see if $message exists before calling output_message function. Or define $message as null before the if/else $message = null; Link to comment https://forums.phpfreaks.com/topic/282934-need-help-with-a-login-page/#findComment-1453781 Share on other sites More sharing options...
eldan88 Posted October 14, 2013 Author Share Posted October 14, 2013 Or define $message as null before the if/else $message = null; Thank you Ch0cu3r. I have put the defined $message to null and it now works. ! Link to comment https://forums.phpfreaks.com/topic/282934-need-help-with-a-login-page/#findComment-1453849 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.