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!