简体   繁体   中英

PHP “Cannot modify header information” error wont go away.

I am getting the following error “Warning: Cannot modify header information - headers already sent by”, and the answers in this question are not solving this problem.

Could anybody please help me?

This is my code.

main_login

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Untitled Page</title>
</head>
<body>
    <form name="form1" method="post" action="checklogin.php">
    <table style="width:300px;border:0;text-align:center;background-color:#CCCCCC">
        <tr>
            <td>
                <table style="width:100%;background-color:#FFFFFF;border:0;">
                    <tr>
                        <td colspan="3"><strong>Member Login </strong></td>
                    </tr>
                    <tr>
                        <td style="width:78px">Username</td>
                        <td style="width:6px">:</td>
                        <td style="width:294px"><input name="myusername" type="text" id="myusername"></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td>:</td>
                        <td><input name="mypassword" type="text" id="mypassword"></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td><input type="submit" name="Submit" value="Login"></td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

checkLogin.php

<?php
$dbhost = 'localhost';
$dbuser = 'myuse';
$dbpass = 'myPassword';
$dbname = 'myDbName';

// This is an example opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die  ('Error connecting to mysql');
mysql_select_db($dbname);


// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM myTable WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword']   = $mypassword;
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

login_success.php

<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
// Check if session is not registered , redirect back to main page. // Put this
code in first line of web page.
<html>
<body>
    Login Successful
</body>
</html>

This is the error

Warning: Cannot modify header information - headers already sent by (output started at /home/latabla1/public_html/MenuSemanal/checklogin.php:1) in /home/latabla1/public_html/MenuSemanal/checklogin.php on line 34.

The connection to the database is fine as when I type wrong login details it goes to echo "Wrong Username or Password";, and I've checked the white spaces, but I cant find what is wrong.

Please help, I have been one whole day trying to fix this.

Make sure checkLogin.php is not stored as a Unicode file (which would include a leading Byte Order Mark).

When using header , there must have been no data yet sent to the browser (as explained on the manual page).

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