为什么我不能摆脱这个错误:警告:无法修改标头信息 - 已经/ Applications / XAMPP /中已经发送的标头(在/Applications/XAMPP/xamppfiles/htdocs/login.php:43处开始输出)第24行的xamppfiles / htdocs / login.php
第24行是一个带有简单括号的线。我寻找额外的空间,因为这是这个错误的常见原因(从我读过的),但我找不到任何。有什么想法吗?
这是我的代码:
<?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>
答案 0 :(得分:4)
你不能在header()函数
之前输出任何内容答案 1 :(得分:1)
标头是HTTP协议(是的,不是PHP)的一种方式来传输有关它将要发送的页面的信息,因此在输出(也就是页面)已经发送之后显然不可能发送标头。这就是你得到这个错误的原因。
要解决此问题,请正确调试代码并找到标头之前的输出位置。我打赌printf("Your username or password are invalid. Please try again.");
我在那里看到了某个地方。