4

I was wondering if anyone can help with posting $_POST values again. Let's say I post form values to post.php where I can access data by $_POST or $_REQUEST variables. But how can I post $_POST to another url let's say post_one.php and access data there?

5
  • Do you want to do this without user intervention, or simply by re-posting another form? Jan 10, 2011 at 17:28
  • 1
    if you need to show that another page to the user, there is no way. Jan 10, 2011 at 17:31
  • with user intervention and user redirection to to another page. but preferably without creating hidden form :-)
    – m1k3y3
    Jan 10, 2011 at 17:33
  • 1
    What I hate for most - it's maximum abstraction level that some folks prefer. "Let's say we have some site and imagine we have to perform whatever action. How to do it?" Is it too hard to say what is that form, what is that another script and what all that mess for? Jan 10, 2011 at 17:41
  • I am sorry for the "abstraction level" but what's the matter what's in the form it can have one text input. If it can be done for one form field it can be done for anything else. I can put into the script anything that will work.
    – m1k3y3
    Jan 11, 2011 at 13:26

6 Answers 6

16

You have to issue a HTTP POST request to your url. A option is to do this using file_get_contents and provide a context. Creating a context is easy using stream_context_create. An example is the following:

$data = http_build_query(
    array(
        'first' => 'your first parameter',
        'second' => 'your second parameter'
    )
);

$headers = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $data
    )
);

$context  = stream_context_create($headers);
$result = file_get_contents($url, false, $context);

This will issue a POST request to $url. $_POST['first'] and $_POST['second'] will be available in your target url.

If you want to re-post all posted variables, replace the first line with:

$data = http_build_query($_POST);
3
  • And if you need to show the result of the new POSTed info, just echo $result. Jan 10, 2011 at 23:56
  • How POSTing data in the same session?
    – xmoonlight
    Nov 26, 2013 at 21:55
  • 2
    This will let the current url stay at this page, can't redirect to the POST target page. How to solve it?
    – Sunry
    Dec 24, 2013 at 6:37
3

If you want to keep the POST data while redirecting the user (i.e., the browser), create a new form containing the POST data (ensure you use htmlspecialchars) and then submit this one (sending it to the new target location).

If this is totally server side, you can just do a simple POST request with cURL or file_get_contents. php.net has a lot of information about this topic.

0
2

Using cURL for example. You can pass $_POST as post array.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/post_one.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_exec($ch);
1

I've presumed that you want the second URL to be user-visible, in which case there are a number of approaches:

  1. Simply add the relevant data into a hidden field in a form and post that form to the new location.

  2. Store the relevant data in a $_SESSSION variable and use a header('Location: /...'); to redirect to the new destination. You can then retrieve the data from the $_SESSION array at the required location.

  3. Add the required variables to a query string and use a header('Location: /...xxx.php?example=true'); to redirect to the new destination. The data will then be available via the $_GET superglobal array.

Of these, I'd recommend the second approach as it will prevent the data from being visible to the end user.

1
  • true, session can be an option.
    – m1k3y3
    Jan 10, 2011 at 17:35
1

It depends what you have in post_one.php, if its just functions and they require the same $_POST var from post.php to work, then you can just include the file:

include 'path/to/post_one.php';

And the functions within it will have access to $_POST.

But if it outputs some kind of html or needs a redirect to it to work properly, then it can get hairy, dont do it like that..

0

In post.php :

session_start();
$_SESSION['POST'] = $_POST;

Then in post_one.php :

session_start();
$_POST = $_SESSION['POST'];

Actually the last line is not needed, you can access the data through $_SESSION['POST'] directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.