I'm struggling to get PHP to write sessions to a database. My issue is that when I override the session handler and proceed to call session_start() I get an error message telling me that the headers were already sent. I've been reading for days with no results.
Common issues that pop up are the BOM and whitespace. I used vi to double check if I have a BOM, which I don't. As for whitespace, I don't see any in my code. However, for testing purposes I removed all blank lines and all tabs. I'll add formatting back in once I solve the issue, but for now, I removed it all. I don't think whitespace is my issue.
The other reason I don't think whitespace is my issue is reading the error message from phpdbg.
[PHP Warning: session_start(): Cannot send session cookie - headers already sent in /usr/local/apache2/htdocs/index.php on line 43]
[PHP Stack trace:]
[PHP 1. {main}() /usr/local/apache2/htdocs/index.php:0]
[PHP 2. session_start() /usr/local/apache2/htdocs/index.php:43]
[PHP Warning: session_start(): Cannot send session cache limiter - headers already sent in /usr/local/apache2/htdocs/index.php on line 43]
[PHP Stack trace:]
[PHP 1. {main}() /usr/local/apache2/htdocs/index.php:0]
[PHP 2. session_start() /usr/local/apache2/htdocs/index.php:43]
If I turn on verbose mode in phpdbg the line before the error is:
[L4 0xb73454f4 ZEND_RETURN C0 <unused> <unused> /usr/local/apache2/htdocs/index.php]
Based on the code I provide below, line 4 is in the open function and is the return value. Line 43 is session_start(). This is where I'm confused. The error message is telling me calling session_start assigns the header and the open function is also trying to assign a header?
From what I understand, the session handler's first call is to the open function. Open is being called, I just don't get why both open and session_start are trying to send headers separately. I don't see any whitespace, there is no BOM. I must be doing something wrong with the session handler?
<?php
$db = pg_connect ( "host='' port='' dbname = '' user='' password=''" );
function open() {
return true;
}
function close() {
return true;
}
function read($id) {
$stmt = 'SELECT data FROM sessions WHERE id = \'' . $id . '\'';
$result = pg_query($stmt);
$numrows = pg_num_rows($result);
if( $numrows > 0 ) {
$row = pg_fetch_array($result, 0, PGSQL_ASSOC);
return $row['data'];
} else
return '';
}
function write($id, $data) {
$access = time();
$stmt = 'REPLACE INTO sessions VALUES (\'' . $id . '\', ' . $access . ',\'' . $data . '\')';
$result = pg_query($stmt);
if( $result)
return true;
return false;
}
function destroy($id) {
$stmt = 'DELETE FROM sessions WHERE id = \'' . $id . '\'';
$result = pg_query($stmt);
if( $result )
return true;
return false;
}
function gc($max) {
$old = time() - $max;
$stmt = 'DELETE * FROM sessions WHERE access < ' . $old;
$result = pg_query($stmt);
if( $result )
return true;
return false;
}
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
session_start();
?>
Note, I'm also attaching the file with the code as well.
index.php