替代 php 重定向方法,适用于其前面的输出

发布于 2024-12-11 08:01:38 字数 239 浏览 0 评论 0原文

所以显然如果你这样做:

<?php
echo 'something';
header("Location: http://something/");
?>

它不会工作,因为标题前面有一个输出...

是否有任何其他替代的 php 重定向方法可以直接从 php 工作而无需安装任何东西,并且即使有输出它仍然可以工作在它之前,这样我就不必担心确保之前没有输出等等......

so apparently if you do this:

<?php
echo 'something';
header("Location: http://something/");
?>

it will not work because there is an output preceding the header...

is there any other alternative php redirection method that works straight from php without installing anything and in which it will still work even if there's an output preceding it so that I don't have to worry about making sure that there is no output before, etc...

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

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

发布评论

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

评论(5

冷月断魂刀 2024-12-18 08:01:38

不会,除非您在输出本身的页面中的 javascript 或 html 标记中执行某些操作(

如果前面的输出存在问题)
您还可以使用输出缓冲,请参阅 ob_start、ob_get
为了解决这个问题

not, unless you do something in javascript or html tags in the page that you output itself

if preceding output is a problem
you can also use output buffering, see ob_start, ob_get
to get around that

梦言归人 2024-12-18 08:01:38

没有其他方法可以进行 php 重定向,但您可以欺骗它,即使使用之前的代码,它仍然可以工作。您将缓冲内容并仅在没有重定向或到达脚本末尾时输出它。注意:在某些情况下这可能会占用大量资源。

ob_start()
....CONTENT...
ob_end_flush();

There is no other way to do a php redirect, but you can fool it to still work even with code prior. You would buffer the content and only output it if there is no redirect or reaches the end of the script. Note: this may be resource heavy in some cases.

ob_start()
....CONTENT...
ob_end_flush();
梦冥 2024-12-18 08:01:38

PHP 中没有其他方法,除了使用 header()...在发送输出之前(标头已发送)...

您可以在设置为 0 秒的 HTML 中使用元刷新,或者JavaScript。

但我不推荐 JavaScript,因为有些人会禁用它。

There are no ways in PHP except using header()... before output is sent (headers be already sent)...

You can either use meta refresh in HTML that is set at zero seconds, or javascript.

But I wouldn't recommend javascript as some will have it disabled.

美人迟暮 2024-12-18 08:01:38

您可以使用 元刷新 标记。

You could use a meta refresh tag.

百变从容 2024-12-18 08:01:38

你明白为什么这是不可能的,对吧?

一旦您回显“某些内容”,您就已经将内容发送到客户端,并且客户端标头的一部分已经发送。您无法追溯修改已发送的标头,也无法对一个 HTTP 请求做出两次响应。

ob_start()ob_end_flush() 将缓冲输出而不是将其发送到客户端,这将允许您解决这个问题,但是
更好的解决方案是:

将逻辑代码与模板分开,以便在您知道不会重定向之前不会向屏幕写入任何内容。

You understand why this is impossible, right?

As soon as you echo "something" you have sent content to the client, and as part of that client headers were already sent. You can't retroactively modify headers you already sent, and you can't make two responses to one HTTP request.

ob_start() and ob_end_flush() will buffer the output instead of sending it to the client, which will allow you to get around this problem, BUT
a better solution would be to:

separate your logic code from your template so that you don't write anything to the screen until you already know you aren't going to redirect.

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