PHP - Pass POST variables with header()?

前端 未结 4 1938
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 10:14

I\'m trying to use the header() function to create a redirect. I would like to display an error message. Currently I\'m sending the message as a parameter through the URL,

4条回答
  •  一个人的身影
    2020-12-14 10:51

    Dan, You could start and store a session in PHP then save the message as a session variable. This saves you from having to transfer the message in an HTTP request.

    Manipulating Sessions

    //Start the session
    session_start();
    
    //Dump your POST variables
    $_SESSION['POST'] = $_POST;
    
    //Redirect the user to the next page
    header("Location: bar.php");
    

    Now, within bar.php you can access those POST variables by re-initiating the session.

    //Start the session
    session_start();
    
    //Access your POST variables
    $temp = $_SESSION['POST'];
    
    //Unset the useless session variable
    unset($_SESSION['POST']);
    

    To read more about sessions, check out: http://php.net/manual/en/function.session-start.php

提交回复
热议问题