有没有“正确的方法”?避免输出缓冲?
我一直在尝试找到启用了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出缓冲是一种解决方法。通过正确构建应用程序流程并注意编码问题和悬空空格可以避免这种情况。但作为此类问题的解决方法,它非常实用。您可以通过将其放在脚本顶部来手动启用它:
您的情况的实际问题可能是 UTF8 BOM。这是文本文件(如 php 脚本)开头的不可见标记。它存在于
之前,因此
ob_start()
无法再提供帮助。它输出 3 个字节,并会阻止发送标头。如果是这样的话,那么 ob_start 将无济于事。但输出缓冲通常也可以通过
.htaccess
启用: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:
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 theob_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: