[Frustrating Issue] with session_set_save_handler()
Posted: Sun Apr 25, 2010 5:13 pm
I'm having a nightmare with a php script that makes use of the session_set_save_handler() function to send session information to my mysql database.
I borrowed a script from a tutorial by tuxradar.com entitled 'Files vs. Database'. Although I modified it slightly, it's worth noting that even an identical script to the tutorial brings about the same issue.
The issue is that I get two warning messages from php on load of the page in the browser:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/otis/session.php:1) in /var/www/otis/session.php on line 47
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/otis/session.php:1) in /var/www/otis/session.php on line 47
The PHP code is as follows:
The results in the resulting MySql table are mixed. The session, for the most part, is created in the table, with the session id string and the time.
However, the warning messages persist, and the $_SESSION[] variables do not get passed to the Data field in the mysql table either. The mysql table was created with:
CREATE TABLE sessions (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, SessionID CHAR(26), Data TEXT DEFAULT '', DateTouched INT);
Any thoughts? I've looked up and down the internet for an answer to this issue but nobody else seems to be mentioning it...
Thanks for any help,
drayfuss
I borrowed a script from a tutorial by tuxradar.com entitled 'Files vs. Database'. Although I modified it slightly, it's worth noting that even an identical script to the tutorial brings about the same issue.
The issue is that I get two warning messages from php on load of the page in the browser:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/otis/session.php:1) in /var/www/otis/session.php on line 47
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/otis/session.php:1) in /var/www/otis/session.php on line 47
The PHP code is as follows:
Code: Select all
<?php
mysql_connect("localhost", "user", "mysqlpass");
mysql_select_db("test");
function sess_open($sess_path, $sess_name) {
return true;
}
function sess_close() {
return true;
}
function sess_read($sess_id) {
$result = mysql_query("SELECT Data FROM sessions WHERE SessionID = '".$sess_id."';");
if (!mysql_num_rows($result)) {
$CurrentTime = time();
mysql_query("INSERT INTO sessions (SessionID, DateTouched) VALUES (".$sess_id.", ".$CurrentTime.");");
return '';
} else {
extract(mysql_fetch_array($result), EXTR_PREFIX_ALL, 'sess');
mysql_query("UPDATE sessions SET DateTouched = ".$CurrentTime." WHERE SessionID = ".$sess_id.";");
return $sess_Data;
}
}
function sess_write($sess_id, $data) {
$CurrentTime = time();
mysql_query("UPDATE sessions SET Data = ".$data.", DateTouched = $CurrentTime WHERE SessionID = ".$sess_id.";");
return true;
}
function sess_destroy($sess_id) {
mysql_query("DELETE FROM sessions WHERE SessionID = ".$sess_id.";");
return true;
}
function sess_gc($sess_maxlifetime) {
$CurrentTime = time();
mysql_query("DELETE FROM sessions WHERE DateTouched + ".$sess_maxlifetime." < ".$CurrentTime.";");
return true;
}
session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
session_start();
$_SESSION['foo'] = "bar";
$_SESSION['baz'] = "wombat";
?>
However, the warning messages persist, and the $_SESSION[] variables do not get passed to the Data field in the mysql table either. The mysql table was created with:
CREATE TABLE sessions (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, SessionID CHAR(26), Data TEXT DEFAULT '', DateTouched INT);
Any thoughts? I've looked up and down the internet for an answer to this issue but nobody else seems to be mentioning it...
Thanks for any help,
drayfuss