1

UPDATE I have modified this script and not it works fine, but i'm still confused.

What I've done is, i've commented out this section in the original script, and now i dont get the headers already sent message:

/*  
include("include_db_connection.php");
$sessionid = session_id();
$update_sessiondetails_query =  "UPDATE t_session SET logout_time=now() WHERE session_id='" .$sessionid. "'";

$update_sessiondetails_result = mysqli_query($db_conn, $update_sessiondetails_query) 
or die('Connected to database, but querying failed');
*/

But what i dont understand is, there are no echos or prints here, all i m doing is updating the database after connecting to db. IS THIS NOT ALLOWED BEFORE A HEADER()....??!! If this is not allowed, what alternative do i've? I would have to update the db with the user logout time anyway!

Thanks again!

ORIGINAL QUESTION & SCRIPT BELOW -

Hi,

I am working on my log out script, and am not able to complete this script successfully as I am getting the headers already sent warning for the below code -

This is my logout script -

<?php
session_start();

include("db_connection.php");   //gets the database connection details like username/host
$sessionid = session_id();      //gets the current logged in user's session ID
$update_sessiondetails_query =  "UPDATE t_session SET logout_time=now() WHERE session_id='" .$sessionid. "'";               //update the database for this user by inserting the logout time

$update_sessiondetails_result = mysqli_query($db_conn, $update_sessiondetails_query) 
or die('Connected to database, but querying failed');

session_unset(); 
session_destroy(); 
                                 // if session uses cookies, clear all cookies
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]);
}
                                //once scripts completes successfully, redirect user to some other page
$homepage_url = "somepage.php";                     
header('Location: '. $homepage_url);

?>

And this is the error that i get - Warning: Cannot modify header information - headers already sent by (output started at...blah blah blah...)

Please let me know why this error is coming. NOTE: While working on my local machine with WAMP, the logout script works fine and doesnt give any error or warning (these are NOT set to OFF in wamp); I get these warnings only when i test using an online web host.

Thanks!

arun nair
  • 3,643
  • 14
  • 41
  • 49
  • you're printing something to the header and trying to set it again. Before header('Location: '. $homepage_url); do you display any text? echo, print, die, etc...? anything that prints to the screen before the header commend will cause this error – Phill Pafford May 23 '11 at 14:04
  • @phillpafford: I am doing a lot of things in page1.php like echo and querying etc.. and the logout.php link is in this page. On clicking logout.php, i get the header warning message. Now, there are no echo statements or any other print stuff in this logout.php script.. I've copied the complete logout script in my question.. that is the only content. – arun nair May 23 '11 at 14:11

2 Answers2

2

When setting a cookie on a page that redirects, the cookie must be set after the call to header('Location: ....');

Such as:

<?php 
header('Location: http://www.example.com/'); 
setcookie('asite', $site, time()+60*60, '/', 'site.com'); 
?>

Source: http://us.php.net/manual/en/function.setcookie.php#96251

onteria_
  • 68,181
  • 7
  • 71
  • 64
  • i changed the order of the cookie part of script and placed the cookie unsetting after the header(), as you said, but i'm still getting the same error/ warning – arun nair May 23 '11 at 14:17
0

It means something has already been output. There might be trailing spaces or line feeds at the beginning or the end of this file or previously included files.

Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
  • is it because my header() is coming at the bottom of this script? am i sending some headers before this function in this particular script? all these are working fine in my local wamp. and this is something that is confusing me. – arun nair May 23 '11 at 14:05
  • You might have output buffering turned on by default on your local wamp and not on your server. Imo, look for leading/trailing spaces in your config files, db connection file, etc. – Denis de Bernardy May 23 '11 at 14:10