Passing POST data from one web page to another with PHP

前端 未结 5 724
天命终不由人
天命终不由人 2020-12-19 05:44

Searching did not find a similar question, so: How can POST data used by PHP to generate one page be passed from that page to another PHP generated page? I have:

相关标签:
5条回答
  • 2020-12-19 06:25

    I recall struggling with this issue long ago, wondering why I simply couldn't redirect with a modified POST header. The reason is a redirect is actually considered a GET.

    Regardless, you need to store the post variables in hidden fields.

    <input type="hidden" name="someValueFromPageOne" value="blah">
    

    I would recommend prefixing all your field names from each form so that its easy to tell them apart during your consolidation phase at the end.

    <input type="hidden" name="pageOne_firstName" value="Joe">
    <input type="hidden" name="pageTwo_streetNumber" value="22">
    

    Edit: As others have mentioned, persisting data using sessions is one possibility, but this becomes a very complex matter of maintaining temporary state which things like page refreshes or using the back button can make difficult to maintain. Unless you're facing an extreme case, it's much easier to persist data using fields since they survive refreshes and other browser behaviour much more easily.

    0 讨论(0)
  • 2020-12-19 06:28

    Wez Furlong recently wrote the php5 version on his blog (titled HTTP post from php, without cURL):

    function do_post_request($url, $data, $optional_headers = null)
    {
    $params = array('http' => array(
    'method' => 'post',
    'content' => $data
    ));
    if ($optional_headers!== null) {
    $params['http']['header'] = $optional_headers;
    }
    $ctx = stream_context_create($params);
    $fp = @fopen($url, 'rb', false, $ctx);
    if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
    }
    $response = @stream_get_contents($fp);
    if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
    }
    return $response;
    }
    

    In the post he mentions that he always has to look up how to do this. Funny because he's one of the core developers!

    0 讨论(0)
  • 2020-12-19 06:36

    Use GET.

    In my opinion, POST requests should modify something (e.g. add records to a database). GET requests should retrieve something (e.g. results of a search query).

    If you want to use POST anyway, look into PHP sessions.

    0 讨论(0)
  • 2020-12-19 06:43

    Sessions are a pain, and if you needed them you'd already have implemented them.

    As @Soviut said above, hidden input fields are probably the way to go for this.

    0 讨论(0)
  • 2020-12-19 06:50

    If you decide to bite off the session route with the dbms option, I've had luck designing a state class to hold this stuff, and serializing an object using JSON to a single large field in the session record.

    0 讨论(0)
提交回复
热议问题