简体   繁体   中英

Not redirecting to home page (index.php)

Is there something wrong about my code? It works but its not redirecting to my index.php it always ended up in the login.php where the form is located.

    <?php
    include 'core/ini.php';

    if (empty($_POST) === false) {
        $username = $_POST ['username'];
        $password = $_POST ['password'];

        if (empty ($username) === true || empty ($password) === true ) {
            $errors[] = 'You need to enter a username and password!';

        } else if (user_exists($username) === false) {
            $errors[] = 'We can\'t find that username. Have you registered?';
        } else if (user_active($username) === false) {
            $errors[] = 'You haven\'t activated your account! ';
        } else {
            $login = login($username, $password) ;
            if ($login === false) {
                $errors[] = 'That username/password combination is incorrect ';
            } else {
                $_SESSION['user_id'] = $login;
                header('Location :index.php');
                exit();
            }
        }
        print_r($errors);
    }
    ?>

thanks!

EDIT *

this is my login.php 
<?php
include 'core/ini.php';

if (empty($_POST) === false) {
    $username = $_POST ['username'];
    $password = $_POST ['password'];

    if (empty ($username) === true || empty ($password) === true ) {
        $errors[] = 'You need to enter a username and password!';

    } else if (user_exists($username) === false) {
        $errors[] = 'We can\'t find that username. Have you registered?';
    } else if (user_active($username) === false) {
        $errors[] = 'You haven\'t activated your account! ';
    } else {
        $login = login($username, $password) ;
        if ($login === false) {
            $errors[] = 'That username/password combination is incorrect ';
        } else {
            $_SESSION['user_id'] = $login;
            header('Location :index.php');
            exit();
        }
    }
    print_r($errors);
}
?>

this is where the process go. I don't know where should I put my start session but I don't know why it works without having an error.

I guess you missed the session_start(); on top of the page since you are storing session. Initiate the session_start(); .

Also does your login() function returns TRUE? Echo something to check whether the function returns TRUE as expected.

您必须在页面顶部使用session_start ,我认为应该在标头定位后删除exit

change header('Location :index.php'); to header('Location: index.php'); That space might be the cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM