1

I have a login.php (input form) and loginvalidation.php(server side validations). I want to display an error message on login.php when input is invalid. I am not able to redirect user to login.php and display error message on it. On Login.php the error message field is <label id="lblErrorMessage" style="color: red;"><?=$errorString?></label>.

2
  • Do you have any code that you've tried so far? In general, you may want to consider using a $_SESSION variable for the error message, here... if you are setting the error message and then redirecting to a new page. (See: php.net/manual/en/reserved.variables.session.php) Commented Mar 3, 2012 at 23:23
  • I'm not at all sure what your question or your problem is. Commented Mar 3, 2012 at 23:25

2 Answers 2

3

I presume you have a form in login.php with

 <form method="post" action="loginvalidation.php">

In loginvalidation.php add

 if ($badLogin) header('location:login.php?err=badLogin');

And catch the err in login.php

$err = $_GET['err'];

I'd recommend to put the validation and form in the same php-page.

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
  //validate and if ok maby redirect (application dependent)
  //if no redirect - we still need to login
  $err = 'no good';
 ?>
 <html>the login form...

regards, /t

Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean by to put the validation and form in the same php-page? You mean to say I should include checking for valid login details in login.php itself?
can you please elaborate it a bit?
Yes, exactly. It's more conveniant to keep track of the code that way. There are circumstances when the opposite is more suiting, but make use of the fact that php can live without separate models/objects.
0

What I do for login forms -

  1. I set the action to (in your case) login.php,
  2. do error checking
  3. a. display the errors if there are any - or -
  4. b. If there aren't any errors I then redirect to the main page.

2 Comments

In my case how will i display error message on login.php (say invalid username/password) after validations are done on loginvalidation.php
See user247245's answer below

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.