jason360 Posted July 11, 2012 Share Posted July 11, 2012 Hey guys, For some reason my login page error messages will not work. It works on a different page, but not on this one. After hours of trouble shooting, I can't figure out what I am doing wrong. Any help would be appreciated. Its just a standard login page. Thanks, JK <?php if($_POST['submit']=='Login') { // Checking whether the Login form has been submitted $err = array(); // Will hold our errors if(!$_POST['username'] || !$_POST['password']) $err[] = 'All the fields must be filled in!'; if(!count($err)) { $_POST['username'] = mysql_real_escape_string($_POST['username']); $_POST['password'] = mysql_real_escape_string($_POST['password']); $_POST['rememberMe'] = (int)$_POST['rememberMe']; // Escaping all input data $row = mysql_fetch_assoc(mysql_query("SELECT user_id,usr,first FROM wk_members WHERE usr='{$_POST['username']}' AND pass='".md5($_POST['password'])."'")); if($row['usr']) { // If everything is OK login $loginok = TRUE; if ($loginok==TRUE) { if ($_POST['rememberMe']=="1") { setcookie('usr', $row['usr'], time()+5184000, '/'); setcookie('user_id', $row['user_id'], time()+5184000, '/'); setcookie('first', $row['first'], time()+5184000, '/'); setcookie('rememberMe', $row['rememberMe'], time()+5184000, '/'); } else if ($_POST['rememberMe']=="") { $_SESSION['usr']=$row['usr']; $_SESSION['user_id'] = $row['user_id']; $_SESSION['first']=$row['first']; $_SESSION['rememberMe'] = $_POST['rememberMe']; } } } else $err[]='Wrong username and/or password!'; $loginok = FALSE; } if($err) $_SESSION['msg']['login-err'] = implode('<br />',$err); // Save the error messages in the session header('Location: http://www.mysite.com'); exit(); } $script = ''; ?> <form action="" method="post" class="reg-form" onsubmit="return formCheck(this);"> <?php if($_SESSION['msg']['login-err']) { echo '<div class="err">'.$_SESSION['msg']['login-err'].'</div>'; unset($_SESSION['msg']['login-err']); } ?> <fieldset> <input class="" type="text" name="username" id="username" value="" size="23" placeholder="Your Email" /> <input class="" type="password" name="password" id="password" value="" size="23" placeholder="Your Password" /> <label><input name="rememberMe" id="rememberMe" type="checkbox" checked="checked" value="1" /> Remember me</label> <div class="submit-button"> <input type="submit" name="submit" value="Login" class="" /> </div> </fieldset> </form> Quote Link to comment Share on other sites More sharing options...
requinix Posted July 11, 2012 Share Posted July 11, 2012 What does "not work" mean? Quote Link to comment Share on other sites More sharing options...
jason360 Posted July 18, 2012 Author Share Posted July 18, 2012 Hey guys, Just wondering if anyone can help me with my submit form. I can`t get the error message to work. After trying a bunch of different things it still wont work. The login works fine, but when I put in a bad email or password it doesn`t show the error message and just forwards through to the header link. Any help would be appreciated. Thanks. if($_POST['submit']=='Login') { // Checking whether the Login form has been submitted $err = array(); // Will hold our errors if(!$_POST['username'] || !$_POST['password']) $err[] = 'All the fields must be filled in!'; if(!count($err)) { $_POST['username'] = mysql_real_escape_string($_POST['username']); $_POST['password'] = mysql_real_escape_string($_POST['password']); $_POST['rememberMe'] = (int)$_POST['rememberMe']; // Escaping all input data $row = mysql_fetch_assoc(mysql_query("SELECT user_id,usr,first FROM wk_members WHERE usr='{$_POST['username']}' AND pass='".md5($_POST['password'])."'")); if($row['usr']) { // If everything is OK login $loginok = TRUE; if ($loginok==TRUE) { if ($_POST['rememberMe']=="1") { setcookie('usr', $row['usr'], time()+5184000, '/'); setcookie('user_id', $row['user_id'], time()+5184000, '/'); setcookie('first', $row['first'], time()+5184000, '/'); setcookie('rememberMe', $row['rememberMe'], time()+5184000, '/'); } else if ($_POST['rememberMe']=="") { $_SESSION['usr']=$row['usr']; $_SESSION['user_id'] = $row['user_id']; $_SESSION['first']=$row['first']; $_SESSION['rememberMe'] = $_POST['rememberMe']; } } } else $err[]='Wrong username and/or password!'; $loginok = FALSE; } if($err) $_SESSION['msg']['login-err'] = implode('<br />',$err); // Save the error messages in the session header('Location: http://mysite.com''); exit(); } $script = ''; ?> <!-- form start --> <form action="" method="post" class="reg-form" onsubmit="return formCheck(this);"> <p class="formMsg"> <?php if($_SESSION['msg']['login-err']) { echo '<div class="err">'.$_SESSION['msg']['login-err'].'</div>'; unset($_SESSION['msg']['login-err']); } ?> <p> <fieldset> <input class="" type="text" name="username" id="username" value="" size="23" placeholder="Your Email" /> <input class="" type="password" name="password" id="password" value="" size="23" placeholder="Your Password" /> <label><input name="rememberMe" id="rememberMe" type="checkbox" checked="checked" value="1" /> <p class="formMsg">Remember me</p></label> <div class="submit-button"> <input type="submit" name="submit" value="Login" class="" /> </div> </fieldset> </form> Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 18, 2012 Share Posted July 18, 2012 This code has an error in it, and shouldn't even run, so you must have edited something before posting it. I don't see a session_start() anywhere. Is this the index file? $err = array(); ... if($err) $_SESSION['msg']['login-err'] = implode('<br />',$err); Will always evaluate to true. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 18, 2012 Share Posted July 18, 2012 Duplicate threads merged. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.