0

The page refreshes instead of going to index.php. The header function seems to be not working, even if I move it anywhere in the code. Does anyone know why this is happening? Would be thankful for the help!

<?php

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

    $Email = $_POST['email'];
    $Password = $_POST['password'];

    $Email = mysqli_real_escape_string($connection, $Email);
    $Password = mysqli_real_escape_string($connection,$Password);

    $query ="SELECT * FROM users WHERE Email = '{$Email}'";
    $select_customer_query = mysqli_query($connection, $query);

    if(!select_customer_query) {
        die("QUERY FAILED". mysqli_error($connection));
    }

    while($row = mysqli_fetch_array($select_customer_query)) {

        $stored_Email = $row['Email'];
        $stored_Password = $row['Password'];
        $User_ID = $row['User_ID'];
        $string ="Logged in as";
        $logoutlink = '/ <a href="resources/includes/back/logout.php">Logout</a>';
        $Firstname_db = $row['First_Name'];
        $Lastname_db = $row['Last_Name'];
        $role_db = $row['Role'];
    } 

    if (password_verify($Password,$stored_Password) ) {

        $_SESSION['FirstName'] = $Firstname_db;
        $_SESSION['LastName'] = $Lastname_db;
        $_SESSION['Email'] = $stored_Email;
        $_SESSION['string'] = $string;
        $_SESSION['logoutlink'] = $logoutlink;
        $_SESSION['User_ID'] = $User_ID;
        $_SESSION['Role'] = $role_db;

        echo '<div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> Your Email and Password was correct.  Click this link to go to <a href="index.php"> Home </a> </div> ';

    } else {

        if ($Email == $stored_Email && $Password !== $stored_Password) {

            echo '<div class="alert alert-danger alert-dismissible" role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <strong>Warning!</strong> Your Password Was Incorrect. Please Try Again. </div> ';

        }
    }  

    if ($Email !== $stored_Email && $Password !== $stored_Password) {

        echo '<div class="alert alert-danger alert-dismissible" role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <strong>Warning!</strong> Your Email or Password Was Incorrect. Please Try Again. </div> ';

    } 

    header('Location: index.php');

}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
steve
  • 57
  • 1
  • 7
  • Can you post the code of your form? I suspect you forgot to specify the `method="post"` attribute – christophetd Mar 29 '16 at 22:38
  • Christophetd, I have that on the form. Must be the ob_start(); – steve Mar 29 '16 at 22:39
  • If you look in the `php error log` you will see and error message – RiggsFolly Mar 29 '16 at 22:42
  • Also there is little point echoing out HTML to the current output buffer if you are then going to redirect to a completely different script/page and therefore throw away whatever you have echo'd – RiggsFolly Mar 29 '16 at 22:44
  • FYI: != isn't the same as !==. The second one compares values and types, while the first one only compares values. The same applies for == and === – DigiLive Mar 29 '16 at 22:45
  • So RiggsFolly, what do you suggest then?? – steve Mar 29 '16 at 22:46
  • _what do you suggest then??_ A few hours with the PHP manual and a few good tutorials – RiggsFolly Mar 29 '16 at 22:47
  • It's ok for everyone to respond, but none of this is helping me. A straight answer would be helpful. There seems to be different answers here!! – steve Mar 29 '16 at 22:47
  • YESSSSS I WORKED IT OUT! – steve Mar 29 '16 at 22:51
  • Possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Qirel Mar 29 '16 at 23:15
  • @Steve please enable error reporting, and check your logs. In more than 90% of the cases, there are already people who have written detailed answers about the issues. Then all you need is Google. ;) – Qirel Mar 29 '16 at 23:16

0 Answers0