Ok, your html form is missing a value for your button. You are defining the name but no value.
<form method="post" action="login.php">
<div class="form-group has-feedback">
<input type="username" name="username" class="form-control" placeholder="Username">
<span class="glyphicon glyphicon-user form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" name="password" class="form-control" placeholder="Password">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
</div>
<!-- /.col -->
<div class="col-xs-4">
<button class="btn btn-primary btn-block btn-flat" type="submit" name="login" value="true">Sign In</button>
</div>
<!-- /.col -->
</div>
</form>
OR, check if ‘login’ array exists
<?php
session_start();
//Getting Input value
if(array_key_exists('login', $_POST)){
$username=mysqli_real_escape_string($conn,$_POST['username']);
$password=mysqli_real_escape_string($conn,$_POST['password']);
if(empty($username)&&empty($password)){
$error= 'Fields are Mandatory';
}else{
//Checking Login Detail
$result=mysqli_query($conn,"SELECT*FROM user WHERE username='$username' AND password='$password'");
$row=mysqli_fetch_assoc($result);
$count=mysqli_num_rows($result);
if($count==1){
$_SESSION['user']=array(
'id' =>$row['id'],
'username'=>$row['username'],
'uname'=>$row['uname'],
'password'=>$row['password'],
'role'=>$row['role']
);
$role=$_SESSION['user']['role'];
//Redirecting User Based on Role
switch($role){
case 'user':
header('location:user/dashboard.php');
break;
case 'management':
header('location:management/index.php');
break;
case 'admin':
header('location:admin/index.php');
break;
}
}else{
$error='Your PassWord or UserName is not Found';
}
}
}
?>
Sorry for asking a stupid question but I have read so many similar questions and couldn’t find where have I done wrong.
I have a login page. If input right username and password, user can get into the landing page of my system. However, if it were wrong username and password, it’ll only stuck at blank page and the address of the page will be localhost/project/login.php but with no error shown although I have wrote prompt error.
Bellows are few links that I have read
- Incorrect username password
- Error login username password
and many more. Below is my code
index.php (where I host my login form)
<form method="post" action="login.php">
<div class="form-group has-feedback">
<input type="username" name="username" class="form-control" placeholder="Username">
<span class="glyphicon glyphicon-user form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" name="password" class="form-control" placeholder="Password">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
</div>
<!-- /.col -->
<div class="col-xs-4">
<button class="btn btn-primary btn-block btn-flat" type="submit" name="login">Sign In</button>
</div>
<!-- /.col -->
</div>
</form>
and here is my login.php
<?php
session_start();
$conn=mysqli_connect('localhost','root','','snapper');
//Getting Input value
if(isset($_POST['login'])){
$username=mysqli_real_escape_string($conn,$_POST['username']);
$password=mysqli_real_escape_string($conn,$_POST['password']);
if(empty($username)&&empty($password)){
$error= 'Fields are Mandatory';
}else{
//Checking Login Detail
$result=mysqli_query($conn,"SELECT*FROM user WHERE username='$username' AND password='$password'");
$row=mysqli_fetch_assoc($result);
$count=mysqli_num_rows($result);
if($count==1){
$_SESSION['user']=array(
'id' =>$row['id'],
'username'=>$row['username'],
'uname'=>$row['uname'],
'password'=>$row['password'],
'role'=>$row['role']
);
$role=$_SESSION['user']['role'];
//Redirecting User Based on Role
switch($role){
case 'user':
header('location:user/dashboard.php');
break;
case 'management':
header('location:management/index.php');
break;
case 'admin':
header('location:admin/index.php');
break;
}
}else{
$error='Your PassWord or UserName is not Found';
}
}
}
?>
Thank you for kind guidance and sorry again for asking about basic question
NOTE / TIP: you should always try debugging. Usually loop conditions is the error. Best thing to do is before the loop, var_dump() the value. If good, move into that loop and dump the values little by little.
Thank you…I tried. But still not working, So I’ll be trying like what you are suggesting
Ok, so I tested the first statement where you check if $_POST[‘login’] is set. Start debugging after that if statement. Var_dump $username and $password and exit; your script and see if you are getting the right results.