How to declare more than one header on PHP

后端 未结 9 2272
梦如初夏
梦如初夏 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: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:

    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):

    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;
    }
    

提交回复
热议问题