n0pt3x
n0pt3x

Reputation: 137

session_start Throws Fatal Error

I'm currently working on a small CMS for my website and I'm getting following error when calling session_start() :

Fatal error: Exception thrown without a stack frame in Unknown on line 0

I'm storing the PDO database connection in the $_SESSION, so I need to call session_start() directly after starting up the script. Here's a snippet :

function initDB($config){ //initalizes the database connection
try{
    @session_start();
}catch (Exception $e){

}
$dsn = 'mysql:dbname='.$config['db'].';host='.$config['host'];
$user = $config['usr'];
$password = $config['pw'];
try {
    $db = new PDO($dsn, $user, $password);
    $_SESSION['db'] = $db;
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

Back traced the error to "@session_start()", so I'm not able to suspress the error with @ or even with a try-catch.

I Hope you could help me. Thanks a lot

Upvotes: 0

Views: 1751

Answers (3)

n0pt3x
n0pt3x

Reputation: 137

So, as I was told, saving a PDO object in the session does invoke that error. I used a workaround, I'm now setting up da connection for each request, instead of storing connections in the session.

Thanks for your help !

Upvotes: 0

Wrikken
Wrikken

Reputation: 70500

You cannot store resources (a PDO object is actually a resource) in a session. On reinitialisation this is broken and throws an exception 'outside' the scope of your PHP file.

Upvotes: 1

Related Questions