April 29, 2012
session_start() – Cannot send session cookie
Question by user1281921
Hi im creating a simple register and login script in PHP but i get these errors when i load the login page.
Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:Userss14phase2index.php:8) in C:Userss14phase2index.php on line 10
Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at C:Userss14phase2index.php:8) in C:Userss14phase2index.php on line 10
From line 6 to 20 of my code
<form action="index.php" method=get>
<h1 align="center" style="color:gray" >Welcome to this simple application</h1>
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
if( $_SESSION["logging"])
{
print_secure_content();
}
else {
if(!$_SESSION["logging"])
{
$_SESSION["logging"]=true;
loginform();
}
Answer by Starx
You have to keep the session_start()
, before you echo or output . You have these HTML which outputs before session_start()
runs.
Your solution:
<?php
/* Make sure this part is at the top */
error_reporting(E_ALL & ~E_NOTICE);
session_start();
?>
<!-- Now other -->
<form action="index.php" method=get>
<h1 align="center" style="color:gray" >Welcome to this simple application</h1>
<?php
if( $_SESSION["logging"])
{
print_secure_content();
}
else {
if(!$_SESSION["logging"])
{
$_SESSION["logging"]=true;
loginform();
}
?>