I have a file sess_function.php
This file lists the various (6) functions used to control sessions..
function _open()
{
try{
// Open the database
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} //try
catch(PDOException $e){
echo "Oops, We're experiencing an error CONNECTING.";
file_put_contents('PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
}
## Kill Connection to Mysql (Using PDO)
function _close(){
$dbh = null;
}
## Read a current session
function _read($id){
try{
// Open the database
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// Begin Query
$id = mysql_real_escape_string($id);
$sth = $dbh->prepare("SELECT data FROM sessions WHERE id = '$id'");
$sth->execute();
}
catch(PDOException $e){
echo "Oops, We're experiencing an error. READING";
file_put_contents('PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
## return '';
}
## Write to current session
function _write($id, $data){
// WRITE Vars
try{
// Open the database
## require_once('dbi.php');
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// Begin Query
$access = time();
$id = mysql_real_escape_string($id);
$access = mysql_real_escape_string($access);
$data = mysql_real_escape_string($data);
$sth = $dbh->prepare("REPLACE INTO sessions VALUES ('$id', '$access', '$data')");
$sth->execute();
}
catch(PDOException $e){
echo "Oops, We're experiencing an error. REPLACING SESSION VAL";
file_put_contents('PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
}
## Destroy current session
function _destroy($id){
try{
// Open the database
## require_once('dbi.php');
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// Begin Query
$id = mysql_real_escape_string($id);
$sth = $dbh->prepare("DELETE FROM sessions WHERE id = '$id'");
$sth->execute();
}
catch(PDOException $e){
echo "Oops, We're experiencing an error.";
file_put_contents('PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
}
## Clean old session data out
function _clean($max){
try{
// Open the database
## require_once('dbi.php');
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// Begin Query
$old = time() - $max;
$old = mysql_real_escape_string($old);
$sth = $dbh->prepare("DELETE FROM sessions WHERE access < '$old'");
$sth->execute();
}
catch(PDOException $e){
echo "Oops, We're experiencing an error. DEL FROM SESSION";
file_put_contents('PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
}
and index.php
////////////////////////////////////////////////////////////////////////////////////
###### Require Database ###### ////////////////////////
####### DB Config Setting #######
$host ='localhost'; //////////////
$dbname ='mydb';//////////
$user ='root'; //////////////
$pass =''; //////////////
/////////////////////////////////
////////////////////////
////////////////////////////////////////////////////////////////////////////////////
###### Start session ////////////////////////
session_start(); ////////////////////////
###### Call Session Functions Include ###### ////////////////////////
require_once('src/cfg/sess_function.php'); ////////////////////////
###### Call function as contained in sess_function() ###### //
session_set_save_handler('_open','_close','_read','_write','_destroy','_clean'); //
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
The table looks like:

The problem is: it gives me no problem, but nothing is stored in the sessions table…
if I UNCOMMENT session_start(); it throws these warnings:
Oops, We're experiencing an error. READING
Warning: session_start() Cannot send session cookie - headers already sent by sess_function.php:33)index.php on line 12
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started sess_function.php:33) in index.php on line 12
Anyone??
The first error message is what you should be looking at. PHP needs to send HTTP header data (a cookie), but there’s already been some output in sess_function.php. Header data cannot be sent after regular output (say,
echo). You print “Oops, We’re experiencing an error. READING”, which is the output PHP complains about. Removing all echo statements will resolve the session problem.Then: the
$dbname,$user,$passand$hostvariables weren’t set.global $dbname, $host, $user, $pass;declarations fixed that.Finally: session_set_save_handler must be called before session_start, because otherwise PHP won’t call the custom functions. The output already sent errors happened because the MySQL errors were triggered before session_start finished running (the MySQL errors occured during session_start)