PHP Redirect with POST data

后端 未结 13 1604
不思量自难忘°
不思量自难忘° 2020-11-22 06:22

I did some research on this topic, and there are some experts who have said that it is not possible, so I would like to ask for an alternative solution.

My situation

13条回答
  •  时光取名叫无心
    2020-11-22 07:19

    You can use sessions to save $_POST data, then retrieve that data and set it to $_POST on the subsequent request.

    User submits request to /dirty-submission-url.php
    Do:

    if (session_status()!==PHP_SESSION_ACTIVE)session_start();
         $_SESSION['POST'] = $_POST;
    }
    header("Location: /clean-url");
    exit;
    

    Then the browser redirects to and requests /clean-submission-url from your server. You will have some internal routing to figure out what to do with this.
    At the beginning of the request, you will do:

    if (session_status()!==PHP_SESSION_ACTIVE)session_start();
    if (isset($_SESSION['POST'])){
        $_POST = $_SESSION['POST'];
        unset($_SESSION['POST']);
    }
    

    Now, through the rest of your request, you can access $_POST as you could upon the first request.

提交回复
热议问题