Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at [duplicate]

Tags:

html

php

mysql

The following warning comes in login page: Its working in localhost but not in remote host

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at on line 8)

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at on line 8)

enter image description here

index.php

<?php
session_start();
if(isset($_SESSION['usr']) && isset($_SESSION['pswd'])){
header('Location: content.php');}
?>
<body>
<center>
<form method='post' action='login.php'>
<!– in this example I link it with login.php to check the password & username–>
<table>
<tr><td>Username:</td><td><input type='text' name='usr'></td></tr>
<tr><td>Password:</td><td><input type='password' name='pswd'></td>
</tr>
<tr><td><input type='submit' name='login' value='Login'></td>
<td><input type='reset' name='reset' value='Reset'></td></tr>
</table>
</form>
</center>
</body>  

content.php

<body>
<a href="resumedownload.php">Click here to Download to Resume</a>
<?php
session_start();
if(!isset($_SESSION["usr"]) || !isset($_SESSION["pswd"])){
 header('Location: index.php');}
include 'logoff.php';
?>
</body>

login.php

<body>
<?php
session_start();
if($_REQUEST['usr']=='suman.trytek' && $_REQUEST['pswd']=='solutions'){
$_SESSION['usr'] = 'suman.trytek';
$_SESSION['pswd'] = 'solutions';
header('Location: content.php');
}
else{
header('Location: index.php');
}
?>
</body>
like image 439
Geetha Avatar asked Feb 03 '14 07:02

Geetha


3 Answers

Move the session_start(); to top of the page always.

<?php
@ob_start();
session_start();
?>
like image 112
Krish R Avatar answered Oct 21 '22 05:10

Krish R


  1. session_start() must be at the top of your source, no html or other output befor!
  2. your can only send session_start() one time
  3. by this way if(session_status()!=PHP_SESSION_ACTIVE) session_start()
like image 35
Frank Avatar answered Oct 21 '22 05:10

Frank


You cannot session_start(); when your buffer has already been partly sent.

This mean, if your script already sent informations (something you want, or an error report) to the client, session_start() will fail.

like image 3
Spoutnik16 Avatar answered Oct 21 '22 05:10

Spoutnik16