Question: I am trying to use the following code to log in to a website. The login works okay, and the session is saved across pages,

I am trying to use the following code to log in to a website. The login works okay, and the session is saved across pages, however whenever a login is successful, index.php is downloaded by the browser

How can this be fixed? If any more information is needed, please say what else is needed and I will update the question.

    Home Page   

NMC

'; $username = filter_has_var(INPUT_POST, 'username') ? $_POST['username']: null; $username = trim($username); $password = filter_has_var(INPUT_POST, 'password') ? $_POST['password']: null; $password = trim($password); $page = filter_has_var(INPUT_POST, 'page') ? $_POST['page'] : null; if (empty($username) || empty($password)){ echo "You must provide a username and password"; } else{ try{ unset($_SESSION['username']); unset($_SESSION['logged-in']); try{ require_once("database_conn.php"); $dbConn = getConnection(); } catch(Exception $e){ throw new Exception("connection error: " . $e->getMessage(), 0, $e); } $querySQL = "SELECT passwordHash from nmc_users WHERE nmc_users.username = :username"; $stmt = $dbConn->prepare($querySQL); $stmt->execute(array(':username' => $username)); $user = $stmt->fetchObject(); //if a record is returned by the statement if($user){ if(password_verify($password, $user->passwordHash)){ $_SESSION['logged-in'] = true; $_SESSION['username'] = $username; if($page == 1) { header('Location:http://unn-16011531.newnumyspace.co.uk/index.php'); } else if($page == 2) { header('Location:http://unn-16011531.newnumyspace.co.uk/editRecord'); } else if($page == 3) { header('Location:http://unn-16011531.newnumyspace.co.uk/credits.php'); } } } else{ echo '

username or password was incorrect. please try again.

'; if($page == 1) { header('Location:http://unn-16011531.newnumyspace.co.uk/index.php'); } else if($page == 2) { header('Location:http://unn-16011531.newnumyspace.co.uk/editRecord'); } else if($page == 3) { header('Location:http://unn-16011531.newnumyspace.co.uk/index.php'); } } } catch(Exception $e){ echo "Record not found: " . $e.getMessage(); } } echo' '; ?>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!