President Obama Posted January 15, 2011 Share Posted January 15, 2011 Say I have my login form which then when person tries to log in with the wrong user name and password and sent straight back to the log in page, how do I send back they got the password and/or username wrong. I read somewhere in some code header("location: index.php&$errorvariable"); or something like that. I haven't tested it, not even sure how to get it to work on the index, would I just use an isset on a get/post to check if it exists then echo the error? Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 15, 2011 Share Posted January 15, 2011 you will need to post some code or we are just guessing on what your are doing, but something along the lines of adding the errors to an array then echoing out all the errors in the array Quote Link to comment Share on other sites More sharing options...
President Obama Posted January 15, 2011 Author Share Posted January 15, 2011 I dont have an error with my code, I'm just talking about a username/password error, like atm if the username/password is wrong it goes back to the login page. I want it to go back but say Username/Password error. There really isn't any code for me to post. Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 15, 2011 Share Posted January 15, 2011 After your sql query to check the database for username and password, lets assume you set the variables for the databse results as $user and $pass and you set the variables for the form posted results as $username and $password $newerror=array(); if ($username!=$user) { $newerror['username']="Incorrect User Name."; } if ($password!=$pass) { $newerror['password']="Incorrect Password."; } if (count($newerror))=='0') { got to logged in page } else { $strError="<div><p>Please check the following and try again:</p><ul>\n"; foreach ($newerror as $error) { $strError.="<li class='indent'>$error</li>\n"; } $strError.='</ul></div>'; header("Location: login.php?error=$strError"); } then on your login page check for the errors with and print them $error=$_GET['error']; if (isset($error) && $error!='') { echo $error; } something like that Quote Link to comment Share on other sites More sharing options...
President Obama Posted January 15, 2011 Author Share Posted January 15, 2011 Oh I see your declaring the variable in the link. I see now. I'll give this a crack. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 15, 2011 Share Posted January 15, 2011 You're much better off not to redirect simply because of a validation error. A header() redirect effectively dumps everything in the $_POST and/or $_GET array, leaving the user to fill out the entire form again, unless you store all that information somehow. Incorporate logic that says "if the form has errors, redisplay with the user-supplied data pre-filled and list the errors, otherwise continue the processing routine." I wrote a (very) basic example a while back that shows how to accomplish that. You can play with it and build off of it if you'd like. <?php if( $_POST['submitted'] == 'yes' ) { //check for hidden field value to indicate form has been submitted $errors = array(); // initialize an array to hold validation errors array_map('trim', $_POST); // trim all $_POST array values if( !empty($_POST['name']) ) { // validate the name field if( !ctype_alpha($_POST['name']) ) { $errors[] = 'Name must be alphabetic characters only.'; // if name has non alpha chars, store error } if( strlen($_POST['name']) < 3 || strlen($_POST['name'] > 20) ) { $errors[] = 'Name must be from 3 to 20 characters.'; // if name has too many/few chars, store error } } else { $errors[] = 'Name is a required field.'; // if name is empty, store error } if( !empty($_POST['number']) ) { // same validations as in name, above. if( !ctype_digit($_POST['number']) ) { $errors[] = 'Number must be numeric.'; } if( strlen($_POST['number']) < 5 || strlen($_POST['number']) > 20 ) { $errors[] = 'Number must be from 5 to 20 digits. It is currently ' . strlen($_POST['number']) . ' digits'; } } else { $errors[] = 'Number is a required field.'; } if( !empty($errors) ) { // if the $errors array is not empty, display the errors to allow the user to correct them and resubmit the form echo "<font color=\"red\">The following errors were detected"; foreach( $errors as $value ) { echo "<br>$value"; } echo '</font>'; } } ?> <form method="post" action=""> <!-- leave action="" attribute empty to submit a form to itself --> Name (3-20 letters): <input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>"><br> Number (5-10 numbers): <input type="text" name="number" value="<?php echo isset($_POST['number']) ? $_POST['number'] : ''; ?>"><br> <input type="hidden" name="submitted" value="yes"> <input type="submit" name="submit" value="Submit"> </form> Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 15, 2011 Share Posted January 15, 2011 I agree with you there but he was already using 2 diff pages, so I just tried to accomodate his request, personally I use the same page to process, I not only display the errors and resubmit the data but I also style the elements so that text boxes have red borders for example where there is an error. I didnt get that indepth with it here as it was only a username and password Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 15, 2011 Share Posted January 15, 2011 I was responding to this, in the OP, actually . . . I read somewhere in some code header("location: index.php&$errorvariable"); or something like that. 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.