Jump to content

How do I return a error?


President Obama

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.