<i id='aobwC'><tr id='aobwC'><dt id='aobwC'><q id='aobwC'><span id='aobwC'><b id='aobwC'><form id='aobwC'><ins id='aobwC'></ins><ul id='aobwC'></ul><sub id='aobwC'></sub></form><legend id='aobwC'></legend><bdo id='aobwC'><pre id='aobwC'><center id='aobwC'></center></pre></bdo></b><th id='aobwC'></th></span></q></dt></tr></i><div id='aobwC'><tfoot id='aobwC'></tfoot><dl id='aobwC'><fieldset id='aobwC'></fieldset></dl></div>

  • <legend id='aobwC'><style id='aobwC'><dir id='aobwC'><q id='aobwC'></q></dir></style></legend>
      <bdo id='aobwC'></bdo><ul id='aobwC'></ul>

        <tfoot id='aobwC'></tfoot>

        <small id='aobwC'></small><noframes id='aobwC'>

      1. 如何在 PHP 上声明多个标头

        时间:2023-11-30

            <legend id='GqQ9I'><style id='GqQ9I'><dir id='GqQ9I'><q id='GqQ9I'></q></dir></style></legend>

            <tfoot id='GqQ9I'></tfoot>

            <small id='GqQ9I'></small><noframes id='GqQ9I'>

              <bdo id='GqQ9I'></bdo><ul id='GqQ9I'></ul>

                    <tbody id='GqQ9I'></tbody>
                  <i id='GqQ9I'><tr id='GqQ9I'><dt id='GqQ9I'><q id='GqQ9I'><span id='GqQ9I'><b id='GqQ9I'><form id='GqQ9I'><ins id='GqQ9I'></ins><ul id='GqQ9I'></ul><sub id='GqQ9I'></sub></form><legend id='GqQ9I'></legend><bdo id='GqQ9I'><pre id='GqQ9I'><center id='GqQ9I'></center></pre></bdo></b><th id='GqQ9I'></th></span></q></dt></tr></i><div id='GqQ9I'><tfoot id='GqQ9I'></tfoot><dl id='GqQ9I'><fieldset id='GqQ9I'></fieldset></dl></div>
                • 本文介绍了如何在 PHP 上声明多个标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想根据用户操作将我的用户发送到不同的页面.所以我在页面顶部做了多个功能,如下所示:

                  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:

                  <?php
                  
                  function one() {
                       header("location: pagea.php");
                  }
                  function two() {
                       header("location: pageb.php");
                  }
                  function three() {
                       header("location: pagec.php");
                  }
                  
                  ?>
                  

                  当然我得到一个错误,因为我正在重新声明标题.起初我认为它会好起来的,因为我将它们包含在函数中并且一次调用任何一个函数.但我仍然得到错误.有没有其他方法可以做到这一点?

                  Of course I get an error because I am re declaring headers. At first I though it was going to be okay since I am containing them inside functions and am calling any one function at a time. But still I get the error. Is there any other way of doing this?

                  推荐答案

                  我想你误解了 HTTP 标头 Location 的作用.

                  I think you misunderstand what the HTTP header Location does.

                  Location 标头指示客户端导航到另一个页面.您不能在每页发送多个 Location 标头.

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

                  另外,PHP 在第一个输出之前发送标头.输出后,您不能再指定任何标头(除非您正在使用输出缓冲).

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

                  如果两次指定相同的标头,默认情况下,header() 将用最新的值替换之前的值...例如:

                  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');
                  

                  将用户重定向到c.php,永远不会经过a.phpb.php.您可以通过将 false 值传递给第二个参数(称为 $replace)来覆盖此行为:

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

                  Location 标头只能指定一次.发送多个 Location 标头不会将用户重定向到页面......它可能会混淆 UA 的废话.另外,请了解代码在发送 Location 标头后会继续执行.因此,按照对 header() 的调用,使用 退出.这是一个正确的重定向功能:

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

                  这篇关于如何在 PHP 上声明多个标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何确定压缩文件的内容长度? 下一篇:在 php 中获取请求的 url 的 Content-Type

                  相关文章

                    <bdo id='i5YJJ'></bdo><ul id='i5YJJ'></ul>
                • <legend id='i5YJJ'><style id='i5YJJ'><dir id='i5YJJ'><q id='i5YJJ'></q></dir></style></legend>

                    <small id='i5YJJ'></small><noframes id='i5YJJ'>

                    <i id='i5YJJ'><tr id='i5YJJ'><dt id='i5YJJ'><q id='i5YJJ'><span id='i5YJJ'><b id='i5YJJ'><form id='i5YJJ'><ins id='i5YJJ'></ins><ul id='i5YJJ'></ul><sub id='i5YJJ'></sub></form><legend id='i5YJJ'></legend><bdo id='i5YJJ'><pre id='i5YJJ'><center id='i5YJJ'></center></pre></bdo></b><th id='i5YJJ'></th></span></q></dt></tr></i><div id='i5YJJ'><tfoot id='i5YJJ'></tfoot><dl id='i5YJJ'><fieldset id='i5YJJ'></fieldset></dl></div>
                    1. <tfoot id='i5YJJ'></tfoot>