0

I created a basic login script in php. on my lampp localhost the redirects work fine. when I load the template to another server the redirects dont work anymore. Can anyone tell me why this is and how to fix it, it would be greatly appreciated. Thanks

Here is the script for admin.php which holds a login form. if $_SESSION['logged_in'] is set then the user should be redirected to the backend.php page every time. But on the server it keeps showing the admin.php page every time. but again when I am on my lampp localhost it works fine.

$users = new Users;

if (isset($_SESSION['logged_in']) === true) {
    header('Location: backend.php');
} else {
    if (empty($_POST) === false) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        if (empty($username) === true || empty($password) === true) {
            $errors[] = 'All fields are required!';
        } else {
            $login = $users->login($username, $password);

            if ($login == 0) {
                $errors[] = 'Username or password incorrect!';
            } else {
                $_SESSION['logged_in'] = true;
                header('Location: backend.php');
                exit();
            }
        }
    }
}
4

3 回答 3

1

I assume the rest of the file contains the actual HTML of the admin page?

Have you verified that the if is getting into the branch you expect. By either using a debugger or something more primitive like die('This should redirect!');

If you have, then the problem is most likely that you have output occurring before your header call. Make sure all errors are showing. It should give you an error telling where the problem is.

It is because of this, it is common to exit() after a header redirect call - to prevent the rest of the page being inadvertently run/displayed.

于 2013-05-01T05:20:31.727 回答
0

Your server likely has a different root folder than your local does. I would try

<?php echo $_SERVER["DOCUMENT_ROOT"]; ?>

and also

<?php echo $_SERVER["SCRIPT_FILENAME"] ?>

That will tell you for sure where you should be.

于 2013-05-01T05:19:52.740 回答
0

使用<?php session_start(); ?>代码的开头。

于 2013-05-01T05:17:16.280 回答