Click here to Skip to main content
16,021,211 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to login using hash password that save to my database but It won't redirect to my main page which is the main.php even though I input the correct password. Please help me, I'm stock here for two days.

Below is my code:

<?php
include("../config/db_connection.php");
session_start();

$query = "SELECT * FROM sys_user WHERE username = :username";
$statement = $db->prepare($query);
$statement->execute(array('username' => $_POST["username"]));

$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
$count = count($rows);

if($count == 1) {
$row = $rows[0];
if(password_verify($_POST["password"], $row['password'])) {
$_SESSION["username"] = $row["username"];
header("Location: main.php");
}
else  {
  header("Location: ../index.php");
}
}
?>


Thank you very much in advanced.

What I have tried:

A lot of manipulation to my code.
Posted
Updated 17-Sep-18 8:24am
Comments
Bryian Tan 4-Sep-18 23:03pm    
have you try update the code to redirect to somewhere else? like https://google.com
Kenjiro Aikawa 5-Sep-18 14:19pm    
Yes I've tried but still not working.

1 solution

?php
//error_reporting(0);

session_start();
require "{$_SERVER['DOCUMENT_ROOT']}/php/connection/db_connection.php";

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

$stmt = $db->prepare("SELECT * FROM tbl_user WHERE userid=:userid LIMIT 1"); 
$stmt->bindValue(':userid', $userid, PDO::PARAM_STR); 
$stmt->execute(); 
$row = $stmt->fetchAll(PDO::FETCH_ASSOC); 

if (count($row) > 0) {
    $hashed_password = $row[0]['password']; 
    $status = $row[0]['status']; 
        if($status == 'Inactive') {
            header("Location: ../../iMonitor_Website/index.php?msg2=wrong");
        }
        elseif(($status == 'Active') && (password_verify($password, $hashed_password))) { 
                $_SESSION["userid"] = $row[0]['userid']; 
                header("Location: ../../iMonitor_Website/admin_dashboard.php"); 
        }
        else {  
        header("Location: ../../iMonitor_Website/index.php?msg=wrong"); 
        }
    } 

?>

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900