Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details

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.

<?php

ini_set("session.save_path", "/home/unn_w16011531/sessionData");
session_start();
// Store Session Data
echo'


<!doctype HTML>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="UTF-8" />
    <title>Home Page</title>
    
</head>

<body>

   <header><h2> NMC </h2></header>     
               

    <nav>
        <!-- The navigation area of the website.
     Contains 4 hyperlinked buttons-->
        <nav>
            <a href="index.php" ><button  class="button">Home</button></a>
            <a href="editRecord.php" > <button  class="button">Admin</button></a>
            <a href="orderRecordsForm.php"> <button class="button"> Select Records</button></a>
            <a href="credits.php" > <button  class="button">Credits</button></a>
        </nav>

    </nav>



';

$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 '<p>username or password was incorrect. please try again.</p>\n';
            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'

</body>
</html>
';
?>
Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.