Is it bad practice to write to $_POST?

后端 未结 7 1584
心在旅途
心在旅途 2021-01-11 17:55

If this is file_1.php


7条回答
  •  無奈伤痛
    2021-01-11 18:25

    You want to use $_SESSION instead.

    $_POST is for information that has been POSTed to the current page and doesn't maintain state between page loads, it will only be populated if you actually post something to the second file when redirecting. If you were to include the second file, rather than redirecting via a header, then what you've done would work since the $_POST variable would still be set.

    $_SESSION will maintain state between pages, so will accomplish what you want when redirecting.

    To use $_SESSION properly, you'll need to call session_start(); first to begin the session. There's more info in the PHP manual.

提交回复
热议问题