0

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: img1

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??

2 Answers 2

1

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, $pass and $host variables 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)

11
  • So, just remove all the echos in the catch{} block?
    – CodeTalk
    Mar 16, 2012 at 13:33
  • Yes. You can still log to a file / db / log server / whatever if you want.
    – abesto
    Mar 16, 2012 at 13:35
  • On second thought: moving session_start before the echo statements will also work, of course.
    – abesto
    Mar 16, 2012 at 13:36
  • I tried commenting out all the echo statements in the catch blocks and it doesnt give me an error... but also doesnt insert into the sessions table either..
    – CodeTalk
    Mar 16, 2012 at 13:36
  • session_start() is before everything exept the function session_set_save_handler
    – CodeTalk
    Mar 16, 2012 at 13:38
0

Why is your session_start() commented? Try turning it on

Are you on auto session start?

1
  • Please see the above added errors if I uncomment session_start();
    – CodeTalk
    Mar 16, 2012 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.