-3

Possible Duplicate:
Headers already sent by PHP

Why can't I get rid of this error: Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/login.php:43) in /Applications/XAMPP/xamppfiles/htdocs/login.php on line 24

Line 24 is a line with a simple bracket. I looked for extra spaces since those are a common reason for this error (from what I've read), but I can't find any. Any ideas?

Here is my code:

<?php

$check = 0;
if (isset($_POST['submit']))
{

$username = htmlentities($_POST['name']);
$username = strtolower($username);
$password = htmlentities($_POST['apw']);
$filename = getcwd() . "/passwd.txt";
$lines = file( $filename , FILE_IGNORE_NEW_LINES );
foreach($lines as $key => $line)
{
list($name, $pw) = explode(':', $line);
if($name == $username && $pw == $password)
{
    $check++;
    break;  
}
}
if ($check == 1){
checkifPlayed($username);
}

else{
printf("Your username or password are invalid. Please try again.");

}

}
$played = 0;
function checkifPlayed($username) {
$results = getcwd() . "/results.txt";
$lines = file( $results , FILE_IGNORE_NEW_LINES );
foreach($lines as $key => $line)
{
    list($name, $score) = explode(':', $line);
    if($name == $username)
    {
    $played++
    break; } 
if ($played != 1){
//Redirect to page
header("location: news.php");}
else {
printf "You've already played and scored $score / 60.";
}
}
}
?>

<form method = "POST" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
Username:<br />
<input type = "text" id="name" name="name" size="20" maxlength="40" />
</p>

<p>
Password:<br />
<input type = "password" id="apw" name="apw" size="20" maxlength="40" />
</p>

<input type="submit" id="submit" name ="submit" name ="submit" value="Log in" />

<p>
<a href="register.php">Register</a></p>
</form>
Community
  • 1
  • 1
Moses89
  • 4,367
  • 3
  • 15
  • 6
  • there are some parse errors in this code. Could you post an **actual one**? thanks. – Your Common Sense Nov 07 '11 at 08:04
  • Do you think storing passwords in plaintext is acceptable? – ThiefMaster Nov 07 '11 at 08:10
  • @Col. Only error I get when this runs is what I have at the very beginning of my question. Everything seems to run fine, I just get that single error. I appreciate your help. – Moses89 Nov 07 '11 at 08:15
  • This is not the actual code or maybe you changed it prior to posting it - line 24 is empty in the above code. – Repox Nov 07 '11 at 08:16
  • @ThiefMaster This is for practice -- I'm a beginner with PHP. – Moses89 Nov 07 '11 at 08:18
  • ALL: I apologize for my error. I made a mistake and posted the wrong code for this. The code above has a parse error instead of a header information error. I'm deeply sorry for this. – Moses89 Nov 07 '11 at 08:25

2 Answers2

5

You mustn't output anything before header() function

  • and, to add to this - you should make sure php does not output anything, either. That is, if some of your functions raise notices (e.g. file() on non-existing file), that will make the problem stay, too. – Aurimas Nov 07 '11 at 08:26
  • Thanks. This saved me hours – James Ashwood Jun 04 '20 at 19:45
1

Headers is a way for the HTTP protocol (yes, not PHP) to transfer information about the page it's about to send, therefore it's obviously impossible to send a header after an output (aka, the page) has already been sent. That's why you get this error.

To solve it, debug your code properly and find where is there an output before the header. I bet on the printf("Your username or password are invalid. Please try again."); I saw somewhere up there.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308