Hi all,

I want to validate username and password in the same page where login form exists. I have this coding and if username and password not matches it redirects to "try_again.php" page. Instead of that i want to show the validation ( "Invalid username or password") in the same page. If username matches to its password it should be redirected to other relevant page.

<?php

$db=mysql_connect('localhost','root','');
mysql_select_db('bank');

$username= $_POST['username'];
$password= $_POST['password'];
$password=md5($password);


$result = mysql_query ("select* from users
							where username='$username' and password='$password'",$db);
				

while($row=mysql_fetch_row($result)){
$user_type=$row[5];

}
session_start();
session_register("user_type");
$_SESSION["user_type"]=$user_type;


		if($user_type=='T'){
			header('Location:teller_page.php');
			
		}else if($user_type=='AO'){
			header('Location:accofficer_page.php');
			
		}else if($user_type=='AS'){
			header('Location:accsup_page.php');
			
		}else if($user_type=='LS'){
			header('Location:loansup_page.php');
			
		}else if($user_type=='CS'){
			header('Location:cashsup_page.php');
			
		}else if($user_type=='GM'){
			header('Location:manager_page.php');	
			
		}else if($user_type=='MBSL'){
			header('Location:welcome.php');
			
		}else if($user_type=='CML'){
			header('Location:reports.php');
			
		[B]}else{
			header('Location:try_again.php');
		}
		
		[/B]
		 
?>

Recommended Answers

All 6 Replies

Hi Vinayakgarg,

Can you tell me how to set the session variable "error" in my login.php page.


Thanks,
Heshan

$_SESSION['error'] = 'Error';

Hi, if I understand (i read it carefully), you want to display and error at the login page.
This will do the job:

<?php
$message = "";
.................
if ($result['username'] != "") {   // IF LOGIN IS SUCCESFULL				
        ............
	Header('Location: successful_login.php');
	exit;
        ...........

} else {
    $message = 'Bad username or password<br />';
    }
?>
....login form
<?php echo $message ?>
.....

Hi Heshan
if i am correct you would want to set error session variable in something like 'check.php' and access it in 'login.php'

$_SESSION['error']='Wrong Username or Password';

Also remember to unset it once you have used it to display a message.
Vinayak

@ riverghost,

have several pages to logged in when a particular user enters correct username and password. Therefore how can i enter following code. Could you please show me in my example.

if ($result['username'] != "") { // IF LOGIN IS SUCCESFULL

............

Header('Location: successful_login.php');
exit;

...........


} else {

$message = 'Bad username or password<br />';

}

@ vinayakgarg,

I tried your coding. But it did not worked. Could you please show me how to enter it in my example given above.

Thank you,
Heshan

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.