The best way IMO - don't redirect at all. :-) Take a scenario - in your case, an error. I see too much of this. Submit, then
"There was an error in your form, you didn't enter a valid email address. Please go back and correct the error."
This is really lame. There are all sorts of ways to patch it up - save info in session variables (which really only works with cookies enabled) is one of them. So if you redirect, that's probably the best way - save your messages in session variables.
But the lameness persists in the case of user errors; it's not friendly and it's not usable. If beloved user enters an error, we want to make it easy for them; bring them back to the very same page they were on and populate their form:
if (!$submit) { output_form(); }
else {
$errors = attempt_to_process_data();
if ($errors) { output_form($errors); }
else { ouput_yay_you_did_it(); }
}
See? No redirects. The simple structure of output_form accepts an error as optional:
function output_form($err) {
$form=null;
if ($err) $form .= "<p class=\"big-and-red\">$err</p>";
// compile your form here
echo $form;
exit;
}
To answer the question of where you store this stuff: If you have a lot of them, use a database, otherwise, you can use a plain text configuration file or an error config file as an include (probably easier), whatever you want. Instead of the numbers, though, your like will be much easier using something easily identified.
include('my-config.php');
<?php
$error_messages = Array (
'bad_email_address' =>
'You have entered an invalid email address. Please make the corrections below.',
'fname_required', => 'Please enter your first name'
);
?>
You will, however, have to declare your array as a global when used within functions.
if ($errors) { output_form($errors); }
function attempt_to_process_data() {
$errors = null;
global $error_messages;
// Do your checks, if an error is found,
$errors .= $error_messages['bad_email_address'];
return $errors;
}
This is but one way to do it, you can do the exact same thing and stay with redirects. It's just handier to build from a user standpoint, which leads you to a self contained script process.