PHP - setcookie(); not working ( Cannot modify header information - headers already sent ) [duplicate]

StackOverflow https://stackoverflow.com/questions/23574062

  •  19-07-2023
  •  | 
  •  

Pregunta

I am getting this error:

Warning: Cannot modify header information - headers already sent by (output started at /removed/loginform.php:2) in /removed/loginform.php on line 24

I understand that I am getting the error when I try to set a cookie, and it's because setcookie can't have any output before it... the problem is, there is no output before it (At least from what I can see). It is saying that "< ?php" is a header, and I don't understand that. If setcookie can only be used in PHP and < ?php is a header, how is it even possible to use setcookie without getting this header error?

My code:

<!--Database Connections-->
<?php include "../../includes/dbpractice_con.php"; ?>

<?php 

//Declare variables
$username = "";
$password = "";
$message = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") { //Retrieve from form when submit is clicked.

    //Escape to prevent SQL injection.
    $username = mysqli_real_escape_string($connection, $_POST['username']);
    $password = mysqli_real_escape_string($connection, $_POST['password']);

    $check = mysqli_query($connection, "SELECT * FROM users WHERE username='$username' AND password='$password'");
    $rowCount = mysqli_num_rows($check);

    if ($rowCount == 1) {

        $cookieExpiration = time() + 757368000; //1 year

        setcookie("username", $username, $cookieExpiration);

    } else {
        $message = "Invalid username or password. <br/>Don't have an account? Click <a href=\"registerform.php\">here</a> to register.<br/><br/>";
    }

}


?>

<html>
¿Fue útil?

Solución

<!--Database Connections--> is HTML output, even if it is just a comment. Remove that and try again.

EDIT: There is also a space between the PHP blocks. Try putting the include in the main PHP block.

Otros consejos

Generally that means you have sent something to client prior to setcookie() call.

Do simple test:

<?php

  ob_start();
  include "../../includes/dbpractice_con.php"; ?>

  # ... rest of your code... #

and before setcookie() call add line

ob_get_clean();

just to be sure that no output was made earlier.

Anything outside of <?php ?> is HTML and is sent to user. So this

<!--Database Connections-->
<?php include "../../includes/dbpractice_con.php"; ?>

outputs <!--Database Connections--> as HTML comment. Another possible output is here

<?php include "../../includes/dbpractice_con.php"; ?>

<?php ...

at least you have new line character between two PHP tags, which is also unwanted output.

You can't have anything to echo, print_r, var_dump data before cookieset() call, check manually for these functions.

Always check all included scripts (with include, require, etc).

BE SURE THEY START WITH <?php WITH NOTHING BEFORE and END WITH ?> FOLLOWING NOTHING.

this is good example:

<?php
  // some lines
?>

these aren't

something<?php ?>

or

<?php ?>something 

or

<?php  ?>

(if file has been saved as UTF-8).

Look at your dbpractice_con.php, check is it saved as ANSI (from notepad check with save as dialog), be sure that everything is between and no one character is before and after the tags. Don't use multiple tags in same script because it increases risk of unwanted output.

Try this:

<?php
// Database connections
include "../../includes/dbpractice_con.php";

//Declare variables
// ... proceed with code ...

If this doesn't work then the problem is probably in dbpractice_con.php

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top