Error Page Dashboard PHP - Cache limiter session start [duplicate]

0

I have problems on my site, in netbeans the site worked perfectly, however when doing hosting on the site br.000webhost.com for tests, it happens that on some pages I get the following message:

Warning: session_start (): Can not send session cache limiter - headers already sent (/ started at /storage/ssd1/343/4724343/public_html/dashboard.php:1) in / storage / ssd1 / 343/4724343 / public_html /dashboard.php on line 6

I searched the internet but could not find anything to help me.

I can not stir. below the session opening code

<?php
// A sessão precisa ser iniciada em cada página diferente
if (!isset($_SESSION))
session_start(); // Essa é a linha na qual o erro se refere

$nivel_necessario = 1;

// Verifica se não há a variável da sessão que identifica o usuário
if (!isset($_SESSION['UsuarioID']) OR ( $_SESSION['UsuarioNivel'] < 
$nivel_necessario)) {
// Destrói a sessão por segurança
session_destroy();
// Redireciona o visitante de volta pro login
header("Location: ../index.php");
exit;
}
?>
    
asked by anonymous 13.02.2018 / 14:03

1 answer

0

Resolving the "headers already sent" problem

The error that a lot of beginner programmers end up in is the famous "Warning: Can not modify header information already sent by ..." or "Warning: Can not send session cookie - headers already sent by ..." and not always easy to figure out what to do to resolve this error (which is not really an error, it's a warning, a warning).

Before solving the problem you need to understand why this error happens.

Why "headers already sent"? Every page on the internet is hosted on a server and your browser "asks" the server for the page, the result (HTML) of the page with an X address ... Then the server moves its chopsticks, interprets the files and begins to respond, sending a response header (the famous header) this header contains information about page encoding, page size, cache lifetime, last update time, and everything that is relevant, from a web page to a browser.

After sending the header, the server sends the HTML of the entire page and your browser starts to put it together for you.

When you read "headers already sent" in the warning, it means that your server has already sent the header and AFTER that sending, you are trying to create or change some information that should be sent in the header.

For example, cookies: are defined before sending the header and sent to the browser INSIDE the header ... If you try to create or change a cookie after the header has been sent you will receive the error warning.

Another example that follows the same logic as Cookies is Session, which are like encrypted cookies that are saved on the server. Every session has a session cookie that is sent to the visitor in order to identify it and keep the values of your session ... If you try to create or remove some value of the session after sending the header will receive the message of error "Warning: Can not send session cookie - headers already sent by ...".

And when the hell did I send the header? I did not do anything! Actually, if you do not use any headers manipulation function, you did nothing and you're getting this error ... But there's an explanation for that!

When it comes to PHP (and I believe the same is true for all other web languages that need to be read by a parser), the header begins to be sent as soon as you enter the first character in the final HTML of the page ... out of PHP code with normal HTML, either inside PHP code with an echo or print ().

<?php
$numero = 3;
$dobro = $numero * 2; // 6

Imagine that on line 1, before the "" there was a space ... Everything outside the "" is HTML, so a space there would be like the 1st character of the HTML causing the header to be sent ... Any function session / cookie / etc. within the PHP block would cause the error.

<?php
echo 'Olá mundo';
session_start(); // Inicio de sessão depois do envio do header?! Problema!

This is another classic case .. The developer attempted to create a session (which will define a new session cookie) after sending the header (because of echo).

Okay, and how do I resolve it? Remember I said you did nothing and you're still getting the error? To solve the problem is the same thing: nothing (beyond normal) needs to be done ... You just need to put all code that works with headers (sessions, cookies, redirects and etc.) before sending any HTML pro character ... Nothing to try set / create a cookie or session after sending a "Welcome!" or submitting your website.

Cookies and sessions, as well as content redirects and encryption, should be uploaded, created, defined and modified BEFORE any HTML ... After all, all HTML can and should depend on these factors.

And before you comment on "but my site needs to send HTML before creating a cookie!" I'll answer you "planning error". :)

I hope fewer people have this problem as of today!

    
13.02.2018 / 14:28