1
I just picked up a project with a friend and came across the following Cookie mistake:
Warning: Cannot Modify header information - headers already send by (output started at...)
I’ll put the code snippet:
<?php
ini_set('output_buffering','On');
ob_start();
require 'admin/util/Conexao.class.php';
require 'admin/util/protecao.php';
if(!empty($_REQUEST['login']) && !empty($_REQUEST['senha'])){
$login = $_REQUEST['login'];
$senha = $_REQUEST['senha'];
$busca = new Conexao("SELECT * FROM convites WHERE login = '$login' AND senha = '$senha'");
$rs = $busca->query();
$total = mysql_num_rows($rs);
if($total != 1){
echo '<script type="text/javascript">alert("Login incorreto")</script>';
echo '<script type="text/javascript">window.location = "login.php"</script>';
}
$row = mysql_fetch_array($rs);
$busca->desconectar();
setcookie('id_convite',$row['id_convite'],time()+3600);
echo '<script type="text/javascript">window.open("formulario.php", "_parent"); </script>';
}elseif(!empty($_REQUEST['cpf']) && !empty($_REQUEST['email'])){
$cpf = anti_injection($_REQUEST['cpf']);
$email = anti_injection($_REQUEST['email']);
$busca = new Conexao("SELECT * FROM convites_alunos WHERE cpf = '$cpf' AND email = '$email' AND confirmado = 'N'");
$rs = $busca->query();
$total = mysql_num_rows($rs);
if($total != 1){
echo '<script type="text/javascript">alert("Login incorreto")</script>';
echo '<script type="text/javascript">window.location = "login.php"</script>';
}
$row = mysql_fetch_array($rs);
$busca->desconectar();
setcookie('id_aluno',$row['id_aluno'],time()+3600);
echo '<script type="text/javascript">window.open("formulario_aluno.php", "_parent");</script>';
}else{
echo '<script type="text/javascript">alert("Login incorreto")</script>';
echo '<script type="text/javascript">window.location = "login.php"</script>';
}
ob_end_flush();
?>
And the error occurs on the line where the cookie is created:
setcookie('id_convite',$row['id_convite'],time()+3600);
Could you help me??? As you can see I already started the buffer and closed, as I saw on the net, but nothing... I used the command:
ini_set('output_buffering','On');
However, the error persists, I am only dependent on it to deliver the project. Thank you.
http://answall.com/questions/4251/erro-do-php-cannot-modify-header-information
– rray
The answer is in the question linked above. You cannot give any output (for example,
echo
) before setting the cookie.– bfavaretto