Warning when trying session_start (); PHP [duplicate]

4

Does anyone know what this warning might be?

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at *caminho*) in *caminho* on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at *caminho*) in *caminho* on line 2

The only thing found in this line 2 of the file is:

session_start();

Thank you in advance!

As a request, posting the index.php file:

<?php

session_start();

require("require/config.php");
require(REQUIRE_PATH . "db.php");
require(CLASS_PATH . "class.login.php");

if(isset($_GET["url"]) && !empty($_GET["url"])){

    $url = $_GET["url"];

    if(in_array($url, $config["secure_pages"])){

        $file = INCLUDE_PATH . $url . ".php";

        if(file_exists($file)){

            include($file);

        } else{

            include(INCLUDE_PATH . "home.php");

        }

    } else{

        include(INCLUDE_PATH . "home.php");

    }

} else{

    include(INCLUDE_PATH . "home.php");

}
}
    
asked by anonymous 09.08.2014 / 00:27

1 answer

12

The error "headers already sent" means that information has already been sent to the client at the time the line is executed.

The session_start should be called at startup, before any HTML, since it changes HTTP headers.

In particular:

  • Can not have any text before <? with session_start ;
  • Can not have any echo , print or anything else that prints text;
  • Can not have any calls to flush
  • If you include a file ( require_once , etc.), then that file also can not print anything;
    • This includes text after the% k of% ending the file. Not even blank lines or spaces.
  • The PHP files in question can not have Byte Order Mark (BOM).
UTF-8 BOM is an (optional) 3-byte set at the beginning of the file that indicates that a file is in UTF-8.

When the text editor sees BOM, it knows what it should do: interpret the rest of the file as UTF-8 and hide BOM. When PHP interprets the file, it does not know what UTF-8 or BOM is. Instead, it reads the file as if it were an ISO-8859. These 3 bytes are therefore 3 characters to send to the browser. Just like text before ?> is sent to the browser, BOM is also because PHP does not notice the difference.

When your browser receives BOM, you know you should ignore it and all browsers (IE, Firefox, Chrome, etc.) do this correctly. Hence the BOM is not visible (not even in the "View source"). However, it's there and it was sent.

In HTTP, headers (headers) must be sent before the content of the page. So when PHP sends text to the client in some way, it must first send the headers. From the time headers are sent, they can not be changed to that page without reloading it. That is, when PHP sends the BOM to the client, it sends the headers first.

The <? changes the headers. However, the headers have already been sent to the client ("headers already sent"). So it does not work.

Removing BOM, PHP stops sending headers before reaching session_start , and as such the function works correctly.

    
09.08.2014 / 00:33