开发者

How do I post $_POST values

I was wondering if anyone can help with posting $_POST values again. Let's say I post form values to 开发者_StackOverflow社区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?


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);


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.


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);


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.


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..


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜