Jump to content

login into two different areas on one login form


I-AM-OBODO
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi all.

I'm trying to get user login into two different areas on one login form based on a criteria. The problem I'm having is that if the correct passwords are provided, everything works fine but when a wrong password is provided, nothing happens, it doesn't even echo the password error alert! What could be wrong and is my code okay?

Thanks

if(isset($_POST['login'])){

$username=$_POST['username'];
$password=$_POST['password'];

$username = stripslashes($username);
$password = stripslashes($password);
$username = $username;
$password = $password;

//$pass = md5($password);

$stmt = $pdo->prepare("SELECT password FROM table WHERE username=:username");
$stmt->bindValue(':username', $username, PDO::PARAM_STR);
$stmt->execute();

if($stmt->rowCount()<1){

echo '<div class="signals"><p class="bg-warning text-center warning"><button type="button" class="close" aria-label="Close"><span aria-hidden="true">×</span></button>INVALID USERNAME OR PASSWORD</div></p>';

}else{
$password = $_POST['password'];
list($hash) = $stmt->fetch(PDO::FETCH_NUM);

if (password_verify($password, $hash)) {
$_SESSION['username'] = $username;

$status1 = "COMPLETED";
$status2 = "UNCOMPLETED";

$stmt = $pdo->query("SELECT status FROM table WHERE username ='$_SESSION[username]'");
$check = $stmt->fetch(PDO::FETCH_ASSOC);
$status = $check['status'];

if(strcmp($status, $status1) == 0){

header("location: completed/index.php");
exit();
}elseif(strcmp($status, $status2) == 0){

header("location: uncompleted/index.php");    
exit();
}else{
    
echo '<div class="signals"><p class="bg-warning text-center warning"><button type="button" class="close" aria-label="Close"><span aria-hidden="true">×</span></button>INVALID USERNAME OR PASSWORD again</div></p>';

}    
}
}
}

 
Link to comment
Share on other sites

  • Solution

 

It doesn't even echo the password error alert!

This is because this else is in the wrong place.

else{
    
echo '<div class="signals"><p class="bg-warning text-center warning"><button type="button" class="close" aria-label="Close"><span aria-hidden="true">×</span></button>INVALID USERNAME OR PASSWORD again</div></p>';

} 

You have have after the if/elseif strcmp statements. It should be after the  if (password_verify($password, $hash))  block.

if(isset($_POST['login']))
{
    $username = stripslashes($_POST['username']);
    $password = stripslashes($_POST['password']);

    $stmt = $pdo->prepare("SELECT password FROM table WHERE username=:username");
    $stmt->bindValue(':username', $username, PDO::PARAM_STR);
    $stmt->execute();

    if($stmt->rowCount()<1)
    {
        echo '<div class="signals"><p class="bg-warning text-center warning"><button type="button" class="close" aria-label="Close"><span aria-hidden="true">×</span></button>INVALID USERNAME OR PASSWORD</div></p>';
    }
    else
    {
        $password = $_POST['password'];
        list($hash) = $stmt->fetch(PDO::FETCH_NUM);

        if (password_verify($password, $hash))
        {
            $_SESSION['username'] = $username;

            $status1 = "COMPLETED";
            $status2 = "UNCOMPLETED";

            $stmt = $pdo->query("SELECT status FROM table WHERE username ='$_SESSION[username]'");
            $check = $stmt->fetch(PDO::FETCH_ASSOC);
            $status = $check['status'];

            if(strcmp($status, $status1) == 0)
            {
                header("location: completed/index.php");
                exit();
            }
            elseif(strcmp($status, $status2) == 0)
            {
                header("location: uncompleted/index.php");    
                exit();
            }  
        }
        else
        { 
            echo '<div class="signals"><p class="bg-warning text-center warning"><button type="button" class="close" aria-label="Close"><span aria-hidden="true">×</span></button>INVALID USERNAME OR PASSWORD again</div></p>';
        } 
    }
}
Edited by Ch0cu3r
Link to comment
Share on other sites

 

 

This is because this else is in the wrong place.

else{    echo '<div class="signals"><p class="bg-warning text-center warning"><button type="button" class="close" aria-label="Close"><span aria-hidden="true">&times;</span></button>INVALID USERNAME OR PASSWORD again</div></p>';}
You have have after the if/elseif strcmp statements. It should be after the  if (password_verify($password, $hash))  block.
if(isset($_POST['login'])){    $username = stripslashes($_POST['username']);    $password = stripslashes($_POST['password']);    $stmt = $pdo->prepare("SELECT password FROM table WHERE username=:username");    $stmt->bindValue(':username', $username, PDO::PARAM_STR);    $stmt->execute();    if($stmt->rowCount()<1)    {        echo '<div class="signals"><p class="bg-warning text-center warning"><button type="button" class="close" aria-label="Close"><span aria-hidden="true">&times;</span></button>INVALID USERNAME OR PASSWORD</div></p>';    }    else    {        $password = $_POST['password'];        list($hash) = $stmt->fetch(PDO::FETCH_NUM);        if (password_verify($password, $hash))        {            $_SESSION['username'] = $username;            $status1 = "COMPLETED";            $status2 = "UNCOMPLETED";            $stmt = $pdo->query("SELECT status FROM table WHERE username ='$_SESSION[username]'");            $check = $stmt->fetch(PDO::FETCH_ASSOC);            $status = $check['status'];            if(strcmp($status, $status1) == 0)            {                header("location: completed/index.php");                exit();            }            elseif(strcmp($status, $status2) == 0)            {                header("location: uncompleted/index.php");                    exit();            }          }        else        {             echo '<div class="signals"><p class="bg-warning text-center warning"><button type="button" class="close" aria-label="Close"><span aria-hidden="true">&times;</span></button>INVALID USERNAME OR PASSWORD again</div></p>';        }     }}
 

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.