session_start() 导致致命错误
我会保持简单。为什么在 PHP 脚本的顶部调用 session_start()
,我在底部得到以下输出:
Fatal error: Exception thrown without a stack frame in Unknown on line 0
发生了什么事?
编辑:一些进一步的细节,因为我已经进一步隔离了问题。
index.php
(摘录):
<?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
(摘录):
<?php
class Gallery {
private $pdo;
public function __construct() {
global $args, $pdo, $request;
$this->pdo = $pdo;
}
...
}
$gallery = new Gallery();
问题出在Gallery$pdo
变量分配为类属性时。当 $pdo 只是一个 PDO 实例时,为什么会导致致命错误?
编辑2:我发现关闭浏览器并重新启动(终止会话)可以抑制该错误。仅当调用 inc/confirm.php
时才会触发该错误。
inc/confirm.php
是用于 18 岁以上确认的脚本。内容很少:
session_start();
if (isset($_GET['mod'])) {
$mod = $_GET['mod'];
$_SESSION[$mod] = '1';
}
header('Location: '.$_SERVER['HTTP_REFERER']);
exit;
如您所见,只需将一个键保存在 $_SESSION
数组中并重定向回原始页面。其中没有关于异常或类解构函数的内容。
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['mod'])) {
$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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设在会话_start 之前没有回显或打印。
如果函数调用前有空格,请尝试将其删除。
在调用 session_start 之前尝试 ob_clean 。我不知道副作用。 :(
尝试
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
没有全局!!!!!!!
为您的数据库连接创建一个包装器(如果您只有一个数据库,则单例类是“OK” - 如果您有多个数据库,则需要修改一个(如果您需要示例,请大声喊))。
那么就使用吧
如果你不厌其烦地重构并且仍然有问题,
。我实际上并不认为这是这里的问题...
我运行
并没有收到任何错误...
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
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
and get no errors...