How to declare more than one header on PHP

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

提交回复
热议问题