Error displaying alerts with $ _SESSION [""] [duplicate]

0

I'm running a test site to train a bit of php , I'm using $_SESSION[""] to show alerts, however, when I go up to a servidor that is not local, alerts stop working.

Project LINK - > link

* Bank file to import into phpi folder

header.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PHP I</title>
    <link rel="icon" href="favicon.png">
    <link rel="stylesheet" type="text/css" href="css/reset.css">
    <link rel="stylesheet" type="text/css" href="css/padroes.css">
</head>
<body>

    <div class="header">
        <a class="title" href="index.php">NarglothStore</a>
        <nav>
            <ul>
                <a href="adiciona-produto.php"><li>Adicionar Produtos</li></a>
                <a href="estoque.php"><li>Estoque</li></a>
                <a href="login.php"><li>LogIn</li></a>
                <a href="logout.php"><li>LogOut</li></a>
                <a href="contato.php"><li>Contato</li></a>
            </ul>
        </nav>
    </div>

LogicaUsuario.php

function verificaUsuario(){
    if (!usuarioEstaLogado()){
        $_SESSION["error"] = "Você não tem acesso a essa funcionalidade";
        header("Location:index.php");
        die();
    };
};

add-product.php

<?php 
require_once("header.php");
require_once("banco-categorias.php");
$categorias = listaCategorias($conexao);
$produto = array("nome" => "", "preco" => "", "descricao" => "", "categoria" => "1");
$usado = "";
require_once("logicaUsuario.php");
verificaUsuario();
?>

index.php

<?php 
    require_once("conexao.php");
    require_once("header.php");
    require_once("logicaUsuario.php");
    require_once("logica-alert.php");
    mostraAlerta("success");
    mostraAlerta("danger");
    mostraAlerta("error");
?>  

logica-alert.php:

<?php
    function mostraAlerta($tipo){
        if (isset($_SESSION[$tipo])){
?>
            <div class="alert-box">
                <p class="alert <?= $tipo ?>"><?= $_SESSION[$tipo] ?></p>
            </div>
<?php
            unset($_SESSION[$tipo]);
        };
    };
?>

Error logs:

Warning: Cannot modify header information - headers already sent by (output started at /home/comunica/public_html/testes/murilo/phpI/header.php:26) in /home/comunica/public_html/testes/murilo/phpI/logicaUsuario.php on line 9

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/comunica/public_html/testes/murilo/phpI/header.php:26) in /home/comunica/public_html/testes/murilo/phpI/logicaUsuario.php on line 2
    
asked by anonymous 06.12.2016 / 13:12

1 answer

2

"Remember that header () must be called before any current output is either normal HTML tags, blank lines in a file, or from PHP "

Remember that header () should be called before any output is sent, either by normal HTML tag * (in our case the header.php html), blank lines in the file or PHP.

The logicaUsuario.php file and any other file that might use the header() file must come before any data output on the screen is either html tag or normal text or blank lines.

    
06.12.2016 / 14:09