php 发送标头后重定向用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7066527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:56:42  来源:igfitidea点击:

Redirect a user after the headers have been sent

phpredirect

提问by GaryDevenay

I understand that so long as data has been sent to the browser then the headers cannot be modified.

我知道只要数据已发送到浏览器,就无法修改标题。

Is there any way (using PHP) that I can perform a redirect to take a user to another page (obviously not using headers)

有什么方法(使用 PHP)可以执行重定向以将用户带到另一个页面(显然不使用标题)

If so could you please point me to some documentation?

如果是这样,你能指点我一些文件吗?

回答by GaryDevenay

Decided to write my own php function which implements a javascript redirect. See the code below.

决定编写我自己的 php 函数来实现 javascript 重定向。请参阅下面的代码。

function redirect($url)
{
    $string = '<script type="text/javascript">';
    $string .= 'window.location = "' . $url . '"';
    $string .= '</script>';

    echo $string;
}

回答by Jim W.

You can use Javascript or a Meta tag. But if you do not want to fix your code to display output after processing then just turn on output_bufferingin the .htaccess file for the site.

您可以使用 Javascript 或 Meta 标签。但是,如果您不想修复代码以在处理后显示输出,那么只需output_buffering在站点的 .htaccess 文件中打开即可。

php_flag output_buffering on

php_flag output_buffering on

Is what you would put in your .htaccess file to turn that on. It will allow redirects even if you output stuff while processing. This is because the output buffer saves the buffer to the end, so no data is sent prior to.

是您将放在 .htaccess 文件中以打开它的内容。即使您在处理时输出内容,它也将允许重定向。这是因为输出缓冲区将缓冲区保存到最后,所以之前没有发送数据。

回答by susmits

I assume output buffering is not an option, since you mention that data has been sent. Use meta redirects, or output JavaScript that takes care of this.

我认为输出缓冲不是一种选择,因为您提到数据已发送。使用元重定向,或输出处理此问题的 JavaScript。

The former could be implemented by adding the following to the <head>section

前者可以通过在该<head>部分添加以下内容来实现

<meta http-equiv="refresh" content="0; url=http://example.com/">

For the latter, writing something like the following could help:

对于后者,编写如下内容可能会有所帮助:

<script type="text/javascript">
    window.location = "http://example.com/";
</script>

Replace http://example.com/with your target URL. Note that the latter might be more wieldly to implement if some data has indeed been sent.

http://example.com/替换为您的目标 URL。请注意,如果确实发送了一些数据,则后者可能更易于实现。

A caveat: Both these methods can be blocked at the client end, but technically, so can 301 and 302 redirects.

警告:这两种方法都可以在客户端被阻止,但从技术上讲,301 和 302 重定向也可以。

回答by Eddy Freddy

Maybe you can use the output bufferbefore you make a decision to redirect the page.

也许您可以在决定重定向页面之前使用输出缓冲区

IMHO otherwise there is only the way to use HTML meta redirect or JavaScript redirect.

恕我直言,否则只有使用 HTML 元重定向或 JavaScript 重定向的方法。

回答by Prescol

This is the php function i use to redirect. If headers haven′t been sent, uses "header(location)", else, uses javascript function.

这是我用来重定向的 php 函数。如果 headers 没有发送,使用 "header(location)",否则使用 javascript 函数。

 function redirect($url)
{
    $baseUri=_URL_;

    if(headers_sent())
    {
        $string = '<script type="text/javascript">';
        $string .= 'window.location = "' . $baseUri.$url . '"';
        $string .= '</script>';

        echo $string;
    }
    else
    {
    if (isset($_SERVER['HTTP_REFERER']) AND ($url == $_SERVER['HTTP_REFERER']))
        header('Location: '.$_SERVER['HTTP_REFERER']);
    else
        header('Location: '.$baseUri.$url);

    }
    exit;
}

回答by Jaroslav ?treit

In my case worked ob_start

在我的情况下工作 ob_start

Javascript solution looks so.. strange

Javascript 解决方案看起来很奇怪

回答by Benjamin Dubois

PHP doe not offer any other way as far as I remember (limitation of the http protocol), but you can do this with javascript, but it is a completely different process : with js, it is the content of the page that tells the browser to perform a redirection, not the server.

据我所知,PHP 不提供任何其他方式(http 协议的限制),但是您可以使用 javascript 来执行此操作,但这是一个完全不同的过程:使用 js,是页面的内容告诉浏览器执行重定向,而不是服务器。