0

I am trying to redirect a user after a logging with saving the session. I am trying to do that using header function in PHP:

<?php
include 'phpconnect.php';

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

    $uname = mysqli_real_escape_string($conn,$_POST['txt_uname']);
    $password = mysqli_real_escape_string($conn,$_POST['txt_pwd']);

    if ($uname != "" && $password != ""){

        $sql_query = "select count(*) as cntAdmin from admindb where adminID='".$uname."' and AdminPassword='".$password."'";
        $result = mysqli_query($conn,$sql_query);
        $row = mysqli_fetch_array($result);

        $count = $row['cntAdmin'];

        if($count > 0){
            $_SESSION['uname'] = $uname;
            header('Location: adminpanel.php');
        }else{
            die("Invalid username and password");
        }

    }

}

?>

Credentials are correct, all other functions (for example registration form with inputting some data into the database) is working. Echo function call works fine if credentials are wrong, so database connection is fine. I suppose something wrong with header, but have no clue.

  • 1
    On browser open bugzilla, network tab and paste here results. You may watch access.log too. – Fran Cerezo Apr 02 '21 at 16:24
  • 1
    **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Apr 02 '21 at 16:27
  • Is there any output prior to this code? – Dharman Apr 02 '21 at 16:28
  • I don't have any output before. – Antsoli Valoranter Apr 02 '21 at 16:37

1 Answers1

0

try this

header("Location: /adminpanel.php");

as a last resort you can use this

echo "<script>";
echo "window.location.href='/adminpanel.php'";
echo "</script>";
Fakt309
  • 821
  • 5
  • 14