-1

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:\Users\s14\phase2\index.php:8) in C:\Users\s14\phase2\index.php on line 10

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Users\s14\phase2\index.php:8) in C:\Users\s14\phase2\index.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();
}
user1281921
  • 161
  • 1
  • 3
  • 12

2 Answers2

1

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();
}
?>
Starx
  • 77,474
  • 47
  • 185
  • 261
0

session_start() must be called before ANY output. So if you put the session_start() call before any of your HTML, there should be no problem.

Marcus Recck
  • 5,075
  • 2
  • 16
  • 26