0

When I try to use the code below:

<?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!";
}

I get the following error:

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

Can someone please help me solve this issue? Thank You

//Update//

This is the rrest of the code, sorry for not adding this earlier:

<?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 Answers1

0

When you get this error it means that you have echoed/printed something already and so the header cannot be sent.

PHP helpfully tells you that headers already sent by…. If you check the code this is referring to you should be able to easily locate the point where you're echoing something.

It's usually:

  • An error/warnings outputted by PHP
  • Debug code left in (print_r, var_dump, etc)
  • An accidental space before <?php

The first two are usually pretty obviously; the third one can be a bit trickier to spot!

I will happily add specifics to this answer if you post some more code.

texelate
  • 2,460
  • 3
  • 24
  • 32