hello everyone , I am beginner in PHP, I have a problem ,

I have a login page (login.php), where user can enter some necessary information ,after submiting ...it goes to another php page(submit.php) and do something there , ... however on that submit.php it perform some business logic validation .. if it finds some error there ... it should redirect to the login.php page with some error message ( may be more than one error messages).... and the error messages should be display in the top of the login.php page .


i don't want to set the error message in the query string in the time of redirection... since there may be a array of error message ..

please help me .... in this regards with some sample code

Dani AI

Generated

Short summary and a practical pattern that ties the existing replies together.

was right to suggest sessions; 's advice to validate on the same page is best for UX when possible; 's numeric-code-in-URL trick can work but exposes state in the URL and is harder to maintain. A simple, robust approach for server-side business validation is: validate in submit.php, save any error messages (and optional old form data) in the session, redirect back with a POST‑Redirect‑GET, then read-and-clear the session in login.php and render the messages. Example pattern:

<?php
// submit.php
session_start();
$errors = [];
// ... perform validations and push strings into $errors ...
if ($errors) {
    $_SESSION['errors'] = $errors;
    $_SESSION['old'] = $_POST; // optional: to repopulate fields
    header('Location: login.php', true, 303);
    exit;
}
// continue on success
?>
<?php
// login.php (top of page)
session_start();
$errors = $_SESSION['errors'] ?? [];
$old = $_SESSION['old'] ?? [];
unset($_SESSION['errors'], $_SESSION['old']);
// later in the template: escape before output
foreach ($errors as $e) {
    echo '<div class="error">'.htmlspecialchars($e, ENT_QUOTES, 'UTF-8').'</div>';
}
// use htmlspecialchars($old['field'] ?? '') to repopulate inputs
?>

Practical cautions: call session_start() before any output; always exit after header('Location:...'); escape messages with htmlspecialchars to avoid XSS; clear the session keys after display to make messages "flash" only once. For heavy traffic or multi‑server setups, ensure session storage is shared (Redis, database) or use stateless flash tokens. This gives a clean, maintainable flow that avoids long query strings and preserves user input after validation failures.

Recommended Answers

All 3 Replies

hello everyone , I am beginner in PHP, I have a problem ,

I have a login page (login.php), where user can enter some necessary information ,after submiting ...it goes to another php page(submit.php) and do something there , ... however on that submit.php it perform some business logic validation .. if it finds some error there ... it should redirect to the login.php page with some error message ( may be more than one error messages).... and the error messages should be display in the top of the login.php page .


i don't want to set the error message in the query string in the time of redirection... since there may be a array of error message ..

please help me .... in this regards with some sample code

hi ..
there is another way to do this ..i.e By using sessions..try to do this by using sessions..

put the validation on the same page. don't let them go to the next page until the first page is completed successfully.

Also if you plan to have a lot of traffic you can use url's and arrays. Simply assign each error message a number and place each number in the url $_GET tag then use something like the following to retrieve the errors.

<?
//$_GET['errs']=1-3-5-6
$errors=explode('-',$_GET['errs']);
$error[0]='This is the first error message<br>';
$error[1]='This is the second possible error message<br>';
$error[2]='Example: Name field too short<br>';
$error[3]='This is another possible error message<br>';
$error[4]='There are some empty fields<br>';
$error[5]='Example error<br>';
$error[6]='Many more errors can be added<br>';
$finalerror=''; //avoid e_notice error
foreach($errors AS $err) {
$finalerror.=$error[$err];
}
//Now to display the errors
echo $finalerror;
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.