头函数重定向到index.php而不是指定的文件(PHP)

时间:2016-03-26 14:18:27

标签: php html header

我目前正在将文件整理到适当的文件夹中,并且出现了问题。在更改代码以组织文件之前,一切正常。现在,每当我尝试登录,而不是重定向到'Staff / staff.php'时,它会重定向到'Staff / index.php'。

代码如下:

<?php
    session_start();

    include("connectdb.php");

    //if the form has been submitted
    if (isset($_POST['submitted'])){
    //get the information out of get or post depending on your form
        $username = $_POST['username'];
        $password = $_POST['password'];

        global $db;

        //sanitise the inputs!
        $safe_username = $db->quote($username);

        //run a query to get the user associated with that username
        $query = "select * from user where username = $safe_username";
        $result = $db->query($query);
        $firstrow = $result->fetch(); //get the first row

        if (!empty($firstrow)) {
            //check the passwords, if correct add the session info and redirect
            $hashed_password = md5($password);

            if ($firstrow['password'] == $hashed_password){
                $_SESSION['id'] = $firstrow['userID'];
                $_SESSION['username'] = $firstrow['username'];
                $_SESSION['fname'] = $firstrow['first_name'];
                $_SESSION['lname'] = $firstrow['last_name'];
                $_SESSION['staff'] = $firstrow['staff'];

                if($firstrow['staff'] == 1) {
                    header("Location:Staff/staff.php");
                    exit();
                } else {
                    //echo "Success!";
                    header("Location:Customer/customer.php");
                    exit();
                }
            } else {
                echo "<h1>Error logging in, password does not match</h1>";
            }
        } else {
            //else display an  error
            echo "<h1>Error logging in, Username not found</h1>";
        }
    }
?>

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="CSS/theme.css">
    </head>
    <body>
        <h1 class="register-title">Aston Animal Sanctuary</h1>
        <div class="register">
            <!--<form method="link" action="staff.php">
                <input type="submit" value="Staff Login">
            </form>-->
            <form action="index.php" method="post">
                <input type="text" class="register-input" name="username" placeholder="Username">
                <input type="password" class="register-input" name="password" placeholder="Password">
                <input type="submit" value="Login" class="register-button">
                <input type="hidden" name="submitted" value="TRUE" /> 
            </form>
            <form method="link" action="register.php">
                <input class="register-button" type="submit" name="register" value="Register">
            </form>
        <div>
        <!--<a href="test1.php" class="button">Test</a>-->
    </body>
</html>

<?php include('View/footer.html'); ?>

标题是问题吗?

修改

我的退出文件也发生了同样的事情。它重定向到'Staff / logout.php'而不是'../logout.php'。在我开始组织文件之前它工作了。

logout.php的代码:

<?php
session_start(); //get the previous session info
session_destroy(); //destroy it

header("Location: ../index.php"); //redirect back to the start

?>

1 个答案:

答案 0 :(得分:0)

你试过了吗?

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

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