How to declare more than one header on PHP

后端 未结 9 2161
梦如初夏
梦如初夏 2020-12-11 02:55

I want to send my users to different pages based on user action. So I made multiple functions at the top of the page like so:



        
相关标签:
9条回答
  • 2020-12-11 03:02

    You're getting that error because your script has already produced output (you used echo/print before calling header()). You need to call header() before your script produces any output.

    From the PHP Manual

    http://php.net/manual/en/function.header.php

    Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

    You'll get the following error when you attempt to call header() after sending any output...

    Warning: Cannot modify header information - headers already sent

    0 讨论(0)
  • 2020-12-11 03:04

    I have a nice solution:

    die(header("Location: someting.php");
    

    You can do that as often as you want. (Nothing else worked for me and to me this is a nice alternative)

    Especially for everyone who has a problem with using multiple options for "header" (on my site it would always just take one, no matter what I did)

    0 讨论(0)
  • 2020-12-11 03:07

    You can try something like that.

    <?php
    function one() {
       $redirect=pagea.".".php;
    
    }
    function two() {
       $redirect=pageb.".".php;
    
     }
    function three() {
     $redirect=pagec.".".php;
    }
    
    
    header("location:".$redirect);
    ?>
    
    0 讨论(0)
  • 2020-12-11 03:07

    For the sake of people who may be coming here from Google, if you're interested in having multiple of the same type of header:

    It is technically possible to have multiple headers that are of the same type passed in PHP. header has a parameter called "replace". From the documentation:

    The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in FALSE as the second argument you can force multiple headers of the same type.

    So you should be able to write:

    header("location: pagea.php");         #You want to overwrite the initial one, so you'd have this one be default, or true
    header("location: pageb.php",false);
    header("location: pagec.php",false);
    

    Of course, if your type of header can't be had multiple times, you'll get an error. In this case you get this error (written this way in Chrome):

    This page isn’t working

    localhost sent an invalid response.

    ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION

    For other headers, however, this method should work fine. The example they supply is:

    header('WWW-Authenticate: Negotiate');
    header('WWW-Authenticate: NTLM', false);
    
    0 讨论(0)
  • 2020-12-11 03:09

    I think you misunderstand what the HTTP header Location does.

    The Location header instructs the client to navigate to another page. You cannot send more the one Location header per page.

    Also, PHP sends headers right before the first output. Once you output, you cannot specify any more headers (unless you are using Output Buffering).

    If you specify the same header twice, by default, header() will replace the previous value with the latest one... For example:

    <?php
    header('Location: a.php');
    header('Location: b.php');
    header('Location: c.php');
    

    will redirect the user to c.php, never once passing by a.php or b.php. You can override this behavior by passing a false value to the second parameter (called $replace):

    <?php
    header('X-Powered-By: MyFrameWork', false);
    header('X-Powered-By: MyFrameWork Plugin', false);
    

    The Location header can only be specified once. Sending multiple Location header will not redirect the users to the pages... It will probably confuse the crap out of the UA. Also, understand that the code continues to execute after sending a Location header. So follow that call to header() with an exit. Here is a proper redirect function:

    function redirect($page) {
        header('Location: ' . $page);
        exit;
    }
    
    0 讨论(0)
  • 2020-12-11 03:11

    Either they aren't all in functions or more than one is being called.

    0 讨论(0)
提交回复
热议问题