Error: “Cannot modify header information - headers already sent by …” [duplicate]

淺唱寂寞╮ 提交于 2019-12-25 05:17:06

问题


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. ;-)


回答1:


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"



来源:https://stackoverflow.com/questions/10788290/error-cannot-modify-header-information-headers-already-sent-by

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!