尝试使用标头(位置)代码时出错

时间:2017-04-03 09:12:43

标签: php

当我尝试使用以下代码时:

<?php
/* User login process, checks if user exists and password is correct */

// Escape email to protect against SQL injections
$email = $mysqli->escape_string($_POST['email']);
$result = $mysqli->query("SELECT * FROM users WHERE email='$email'");

if ( $result->num_rows == 0 ){ // User doesn't exist
    header("location: error.php");
    $_SESSION['message'] = "User with that email doesn't exist!";
}

我收到以下错误:

Warning: Cannot modify header information 
- headers already sent by 
(output started at /some-server/Pages/index.php:10) 
in /some-server/Pages/login.php on line 9

有人可以帮我解决这个问题吗? 谢谢

//更新//

这是代码中最热门的,抱歉不提前添加:

<?php 
/* Main page with two forms: sign up and log in */
require 'db.php';
session_start();
?>
<!DOCTYPE html>
<html>
<head>
   <title>Sign-Up/Login Form</title>
   <?php include 'css/css.html'; ?>
</head>

<?php 
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
    if (isset($_POST['login'])) { //user logging in

        require 'login.php';

    }

    elseif (isset($_POST['register'])) { //user registering

        require 'register.php';

    }
}
?>

1 个答案:

答案 0 :(得分:0)

当您收到此错误时,表示您已经回显/打印了某些内容,因此无法发送标题。

PHP有用地告诉你headers already sent by…。如果您检查这是指的代码,您应该能够轻松找到您回复某些内容的位置。

通常是:

  • PHP输出的错误/警告
  • 调试代码(print_rvar_dump等)
  • <?php
  • 之前的意外空格

前两个通常很明显;第三个可能有点棘手!

如果您发布更多代码,我将很乐意为此答案添加细节。