Error displaying alerts with $_SESSION[""]

Asked

Viewed 103 times

0

I’m running a test site to train a little bit of php, am using $_SESSION[""] to show alerts, however, when climbing to a servidor non-local alerts stop working.

PROJECT LINK-> https://drive.google.com/file/d/0B8AhqmUgCKAfTWlSdW5TMkFzVmM/view?usp=sharing

*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
  • 1

    Need to find out if any error happens, in the first script add these two lines: ini_set('display_errors', true); error_reporting(E_ALL);

  • You need to see which text output the header.php ago.

  • @rray, how do I check and correct ?

  • The second error means that a text output (html, BOM, echo, Warning or error message) came out when the header was already sent. You need to have any of these before a call from header()

  • I’m not making it :/

  • 1

    Places the header.php content

  • No index.php lets include/require do header.php before the conexão.php

  • did not work, I also tried to exchange the require_once by include on all pages, but it did not work

  • I think the way is to test the index with a include/require so you isolate the problem easier

  • @rray, I did it, however, in all cases gave the same problem... I will zip the project and put the link here, would you like to test it there ?

Show 5 more comments

1 answer

2


  • 2

    A tip, always search if there is no answer http://answall.com/search?tab=votes&q=session_start%20send%20session%20cache%20limiter%20headers%20%already sent

  • I asked the question because I did not find a similar on the site, but thanks anyway @Guilhermenascimento

  • didn’t work, I did everything with header(), but it didn’t work

  • puts header.php as the last file to be inserted

Browser other questions tagged

You are not signed in. Login or sign up in order to post.