简体   繁体   English

PHP标头位置不起作用/ HTTP状态无效:更改

[英]PHP Header location not working / no HTTP Status: change

I'm facing a problem using Headers in PHP. 我在PHP中使用标头时遇到问题。 header('location: http:google.com'); is not working. 不管用。 This is my code: 这是我的代码:

if(!headers_sent()){
    header("location: http://google.com");
    exit;
}

HTML Data...

Script is exiting but header() is not working. 脚本正在退出,但header()无法正常工作。

  • I've already checked in my php.ini for output_buffer param and it's set to 4096. 我已经在php.ini中检查了output_buffer参数,并将其设置为4096。
  • There are not extra lines above PHP opening tag PHP开头标记上方没有多余的行
  • and my script is encoding in UTF-8 w/o BOM. 而且我的脚本使用UTF-8(不带BOM)进行编码。
  • All errors are enabled but I'm getting no errors/warnings/notices. 启用了所有错误,但没有收到任何错误/警告/通知。 What is happening? 怎么了?
  • Environment: PHP 7.0.27 and NGINX. 环境:PHP 7.0.27和NGINX。

Later attempts and debugging steps concluded: 后来的尝试和调试步骤结束了:

  • header("location: http://google.com";, true, 302); made no difference 没有区别
  • header("Refresh: ...") did succeed. header("Refresh: ...")确实成功。
  • HTTP response debugging (after being asked a couple of times): HTTP响应调试(多次询问):

     StatusCode : 200 StatusDescription : OK Content : <br /> <b>Notice</b>: Undefined index: HTTP_ACCEPT_LANGUAGE in ... RawContent : HTTP/1.1 200 OK Transfer-Encoding: chunked Connection: keep-alive Pragma: no-cache Cache-Control: no-store, no-cache, must-revalidate Content-Type: text/html; charset=UTF-8 Date: Fri, 18 May 201... Forms : {} Headers : {[Transfer-Encoding, chunked], [Connection, keep-alive], 

Unanswered still: 仍未回答:

  • header("Status: 303 etc"); vs. header('HTTP/1.1 301 Moved Permanently'); header('HTTP/1.1 301 Moved Permanently'); from the duplicate 从重复

  • State of cgi.rfc2616_headers cgi.rfc2616_headers

  • PHP SAPI PHP SAPI

  • Running the sample code outside the phar. 在phar之外运行示例代码。

Right, this is not so much as a fix to the problem but a work around to get redirects working client side while you're on your development server. 没错,这不只是解决问题的方法,而是在开发服务器上时在客户端工作的一种重定向方法。 I wrote up a middle ware function to generate the redirect headers for you, simply add it to your code then call Redirect("URL"); 我编写了一个中间件函数来为您生成重定向标头,只需将其添加到您的代码中,然后调用Redirect("URL"); .

<?php

function Redirect($location, $delay=0) {
    // Check to see if the header are already sent.
    if(!headers_sent()) {
        // Check to see if we want Refresh because Location isn't working.
        if(defined('REDIRECT_ALTERNATIVE')) {
            // We want Refresh, let's go!
            header("Refresh: $delay; URL=\"$location\"");  
            exit;
        }

        // We want to do this server side, do it!
        header("Location: $location");
        exit;
    }
}

Now that you have this function all that you need to do is in a file, preferably a constants or config file, use the code define('REDIRECT_ALTERNATIVE', true); 现在,您已经拥有此功能,只需在文件(最好是常量或配置文件)中,使用代码define('REDIRECT_ALTERNATIVE', true); then that will enable client side redirecting, when you are done simply comment or remove that definition and it will switch back to server side. 然后,这将允许客户端重定向,完成后,只需注释或删除该定义,它将切换回服务器端。

Again this is not a solution as much as it is a work around, but after hours of testing with the OP this is what we managed to come up with. 再一次,这不是一个解决方案,而是一个可行的解决方案,但是在经过OP数小时的测试之后,这才是我们设法解决的问题。

Edit: For anyone who is wondering what the actual problem is, the OP's development server, for whatever reason, is always setting the http code to 200, even if we changed it manually, since I had no way to access his pc and debug his setup I came up with a workaround that avoids the need for his server to manage the redirects. 编辑:对于任何想知道实际问题是什么的人,OP的开发服务器,无论出于何种原因,总是将http代码设置为200,即使我们手动更改了它,因为我无法访问他的pc并调试他的pc。设置过程中,我想到了一种解决方法,该方法可以避免他的服务器管理重定向。

尝试这个。

     header("Refresh: $seconds; URL=\"$location\"");  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM