session_start() 导致致命错误

发布于 2024-10-21 04:52:38 字数 1505 浏览 13 评论 0原文

我会保持简单。为什么在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

策马西风 2024-10-28 04:52:38

我假设在会话_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

檐上三寸雪 2024-10-28 04:52:38

没有全局!!!!!!!

为您的数据库连接创建一个包装器(如果您只有一个数据库,则单例类是“OK” - 如果您有多个数据库,则需要修改一个(如果您需要示例,请大声喊))。

那么就使用吧

class Gallery {
    private $pdo;
    public function __construct() {

        $this->pdo = DBObj::getInst();
    }
    ...
}
$gallery = new Gallery();

如果你不厌其烦地重构并且仍然有问题,

。我实际上并不认为这是这里的问题...

我运行

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();

并没有收到任何错误...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文