有没有“正确的方法”?避免输出缓冲?

发布于 2024-10-29 11:15:38 字数 1294 浏览 3 评论 0原文

我一直在尝试找到启用了 output_buffering 的主机,但我的主机 atm 没有启用它。想要抓住他是不可能的……所以……我需要找到解决办法,或者找到一个新的宿主。我想在决定是否有“正确的方法”或不需要打开output_buffering的方法之前我可能会问一下。

这是我制作的一个脚本,如果没有 output_buffering 则无法运行:

<?php
session_start();
if (isset($_SESSION['authenticated'])) {
    if (isset($_GET['time']) && isset($_GET['user']) && isset($_GET['pass']) && isset($_GET['ip'])) {
        require("config.php");
        $currentTime = time();
        $time = mysql_real_escape_string($_GET['time']);
        $user = mysql_real_escape_string($_GET['user']);
        $pass = mysql_real_escape_string($_GET['pass']);
        $ip = mysql_real_escape_string($_GET['ip']);
        mysql_query("INSERT INTO `deleted` (timeDeleted, timeLogged, username, password, ip) VALUES('$currentTime','$time','$user','$pass','$ip')") or die(/*mysql_error() . */" Error code: 1");
        mysql_query("DELETE FROM `flagged` WHERE `timeLogged`='$time'") or die(/*mysql_error() . */"Error code: 2");
        mysql_close($con);
        include("flagged.php");
    }
} else {
    header("Location: index.php");
    exit;
}
?>

它失败是因为 include("flaged.php"); 永远不会被包含,或者可能会被包含,但我只是从未看到它。

所以我的问题是:我该怎么做才能让它在没有output_buffering的情况下工作? output_buffering 是不好的/坏的做法吗?

I've been trying to find a host that has output_buffering enabled and my host atm doesn't have it enabled. It's impossible to get a hold of him... So... I need to find a solution or get a new host. I thought I might ask before I decide if there was a "right way" or a way that output_buffering doesn't have to be on.

Here's a script I made that fails to function without output_buffering:

<?php
session_start();
if (isset($_SESSION['authenticated'])) {
    if (isset($_GET['time']) && isset($_GET['user']) && isset($_GET['pass']) && isset($_GET['ip'])) {
        require("config.php");
        $currentTime = time();
        $time = mysql_real_escape_string($_GET['time']);
        $user = mysql_real_escape_string($_GET['user']);
        $pass = mysql_real_escape_string($_GET['pass']);
        $ip = mysql_real_escape_string($_GET['ip']);
        mysql_query("INSERT INTO `deleted` (timeDeleted, timeLogged, username, password, ip) VALUES('$currentTime','$time','$user','$pass','$ip')") or die(/*mysql_error() . */" Error code: 1");
        mysql_query("DELETE FROM `flagged` WHERE `timeLogged`='$time'") or die(/*mysql_error() . */"Error code: 2");
        mysql_close($con);
        include("flagged.php");
    }
} else {
    header("Location: index.php");
    exit;
}
?>

It fails because include("flagged.php"); never gets included, or it might but I just never see it.

So my question is: What can I do to get this to work without output_buffering? And is output_buffering bad/bad practice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

淡看悲欢离合 2024-11-05 11:15:38

输出缓冲是一种解决方法。通过正确构建应用程序流程并注意编码问题和悬空空格可以避免这种情况。但作为此类问题的解决方法,它非常实用。您可以通过将其放在脚本顶部来手动启用它:

<?php
   ob_start();

您的情况的实际问题可能是 UTF8 BOM。这是文本文件(如 php 脚本)开头的不可见标记。它存在于 之前,因此 ob_start() 无法再提供帮助。它输出 3 个字节,并会阻止发送标头。如果是这样的话,那么 ob_start 将无济于事。

但输出缓冲通常也可以通过 .htaccess 启用:

php_value output_buffering On

Output buffering is a workaround. It can be avoided by structuring the application flow correctly and taking care with encoding issues and dangling whitespace. But as workaround for such issues, it is quite functional. And you can manually enable it by placing this on top of your script:

<?php
   ob_start();

The actual problem in your case might be the UTF8 BOM. This is an invisible marker at the start of text files (like your php script). It lives before the <?php and thus the ob_start() can't help anymore. It outputs 3 bytes and would prevent headers from getting sent. If that's the case, then ob_start won't help.

But output buffering can often be enabled via .htaccess too:

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