3

I have the following code which will check to see if the following fields produced an error:

//Input Validations
if($user_name == '') {
    $errmsg_arr[] = 'Username missing';
    $errflag = true;
}
if($user_password == '') {
    $errmsg_arr[] = 'Username Password missing';
    $errflag = true;
}
if($insp_name == '') {
    $errmsg_arr[] = 'Inspector Name missing';
    $errflag = true;
}
if($insp_email == '') {
    $errmsg_arr[] = 'Inspector Email missing';
    $errflag = true;
}
if($confirm_password == '') {
    $errmsg_arr[] = 'Confirm Password missing';
    $errflag = true;
}       

if ($user_password != $confirm_password) { 
        $errmsg_arr[] = 'The password which you have entered do not match';
    $errflag = true;
   }

$result = mysql_query("SELECT * FROM members WHERE `email` = '$insp_email' or `login` = '$user_name' LIMIT 1" ); 
$exist = mysql_fetch_row($result); 
    if ($exist !==false ) { 
       $errmsg_arr[] = 'That email is already registered.';
       $errflag = true;
       }

//If there are input validations, redirect back to the registration form
if($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: accountinfo.php");
    exit();
}

If an error was produced once the user is redirected back to the registration page, no error message displays. So the user has no idea why they were redirected back to the registration page. How can I get it so that once they are redirected back to the registration page, it will display an error message stating what the problem was? Ex. Username was missing, or email already registered. I want to display the error message pertaining to the error that caused them to be redirected back to the registration page.

3 Answers 3

6

The most common approach is to use flash messages. The idea is to store a flash message using session. If the message is exists in the current session, you show it's contents and delete it (so we are sure it is a "one-time" message):

// Register page code:

...
$_SESSION['flash_message'] = 'An error has occurred on the previous page! You\'ve broken something!';
...

After redirect:

if (!empty($_SESSION['flash_message'])) {
    // let's show our message to a user
    echo $_SESSION['flash_message'];
    // and don't forget to erase it from session
    unset($_SESSION['flash_message']);
}

I would suggest to write your own class for this purpose, e.g.:

FlashMessage::set('error', 'the error message itself');
FlashMessage::get('error'); // will perform the unset
// or
FlashMessage::render('template', 'error'); // would render error template and show error message immediatelly if exists... etc.
3
  • I added the code that you suggested, but it doesn't display a message.
    – Jason
    Jul 26, 2011 at 15:51
  • No, sessions is started. Here is the code for my registration page form: session_start(); if (array_key_exists('flash_message', $_SESSION)) { // let's show our message to a user // and don't forget to erase it from session unset($_SESSION['flash_message']); } And I replaced my code: $_SESSION['ERRMSG_ARR'] = $errmsg_arr; with your code: $_SESSION['flash_message'] = 'Error is happened on prevoius page! You\'ve broke something!';
    – Jason
    Jul 26, 2011 at 15:57
  • 1
    My example is a boilerplate. I hope you didn't copy-paste it entirely "as-is"?
    – Nemoden
    Jul 26, 2011 at 15:58
0

Add a error_message or error_messages GET parameter to accountinfo.php.

Then, if there is an error, redirect to:

header("location: accountinfo.php?error_message=$some_message");

I note that there can be more than one error, so you need to decide if you want to display them all at once.

An alternative solution is to store the list of errors in the session:

$_SESSION['registration_errors'] = $errmsg_arr;

Also note that your code is vulnerable to SQL injection.

1
  • 1
    For the OP: the route with adding error_message to query_string might be dangerous. Use it only if you are sure you will not exceed maximum URL length. I, myself, have ran into this issue several times. It happened when user submitted a long text and program realised there is an error in this text and returned user back with ?errormsg=LOOONG_TEXT. I wanted to quote this text with my highlights in order to show what exactly user wrote wrong. Thus, if you go this way, be cautious.
    – Nemoden
    Jul 26, 2011 at 15:53
0
print "You have input errors. These are:";
print "<BR><UL>";
for ($i=0; $i<sizeof($errmsg_arr); $i++) 
{
    print "<LI>$errmsg_arr[$i]";
}

insert below of your code and it will show what field/s are missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.