session_start() causes fatal error
I'll keep this simple. Why calling session_start()
at the top of my PHP script, I'm getting this output at the bottom:
Fatal error: Exception thrown without a stack frame in Unknown on line 0
What's going on?
EDIT: Some further details as I've isolated the problem further.
index.php
(excerpt):
<?php
session_start();
require_once('inc/database.php');
require_once('inc/gallery.php');
...
database.php
:
<?php
try {
$dsn = 'mysql:host=localhost;dbname=tees_db';
$pdo = new PDO($dsn, '[username removed]', '[password removed]');
}
catch (PDOException $e) {
header('HTTP/1.1 503 Service Unavailable');
die('There was an error connecting to the database.');
}
gallery.php
(excerpt):
<?php
class Gallery {
private $pdo;
public function __construct() {
global $args, $pdo, $request;
$this->pdo = $pdo;
}
...
}
$gallery = new Gallery();
The problem comes in the Gallery
class when I try and assign the global $pdo
variable as a class property. Why would this cause a fatal error when $pdo
is just a PDO instance?
EDIT 2: I've found closing my browser and re-launching (killing the session) suppresses the error. The error is only triggered when inc/confirm.php
is called.
inc/confirm.php
is a script used in over-18 confirmation. The contents are minimal:
session_start();
if (isset($_GET['mo开发者_开发百科d'])) {
$mod = $_GET['mod'];
$_SESSION[$mod] = '1';
}
header('Location: '.$_SERVER['HTTP_REFERER']);
exit;
As you can see, simply saves a key in the $_SESSION
array and redirects back to the original page. Nothing about exceptions or class de-constructors in there.
I assume there is no echo or printing before session _start.
If there is any blank space before the function call,try to remove it.
Try ob_clean before calling session_start . i dont know the side effects. :(
Try
NO GLOBALS!!!!!!!
create a wrapper for your database connection (singleton class is 'OK' if you only have one db - if you have more then a modified one is required(holla if you need example)).
then just use
class Gallery {
private $pdo;
public function __construct() {
$this->pdo = DBObj::getInst();
}
...
}
$gallery = new Gallery();
if you can be bothered to refactor and still have probs just holla.
I don't actually think that is the problem here though...
I ran
session_start();
try {
$dsn = 'mysql:host=localhost;dbname=DB';
$pdo = new PDO($dsn, 'UN', 'PW');
}
catch (PDOException $e) {
header('HTTP/1.1 503 Service Unavailable');
die('There was an error connecting to the database.');
}
class Gallery {
private $pdo;
public function __construct() {
global $args, $pdo, $request;
$this->pdo = $pdo;
}
}
$gallery = new Gallery();
and get no errors...
精彩评论