Error: “Cannot modify header information - headers

2019-08-24 17:33发布

This question already has an answer here:

I'm developing a website and I purchased MAMP PRO. When I try to login through login_user.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[] = 'username/password combination is incorrect!';
}else {

   //set user session 
$_SESSION['user_id'] = $login;
//redirect user to home
header('Location: home.php');
exit();
    }

    }
print_r($errors);
    }
     ?>

the user should be redirected to home.php.

It used to work perfectly on the server I was using before but now I get just a white page (it stay basically on login_user.php) when I test it on my local machine. The connection to the database works fine. Does Header work properly on MAMP? I'm able to set the session and destroy it in logout.php but both files don't redirect after having set or destroyed the session. Any clue?

UPDATE: I get this error:

Warning: Cannot modify header information - headers already sent by (output started at   /Applications/MAMP/htdocs/mywebsite/core/database/databaseconn.php:2) in /Applications/MAMP/htdocs/mywebsite/login_user.php on line 26

FIXED IT!!! the problem was that in databaseconn.php I left blank the first line before <?php
I can't believe it. ;-)

标签: php mamp
1条回答
smile是对你的礼貌
2楼-- · 2019-08-24 18:06

the problem was that in databaseconn.php I left blank the first line before <?php you may want to refer to this link: "php-header-redirect-not-working"

查看更多
登录 后发表回答