How to place the values ​​of $ _POST

I was wondering if anyone could help with posting $_POST values ​​again. Let's say I submit post.php form values where I can access the data using the variables $ _ POST or $ _ REQUEST . But how can I send $ _ POST to another URL, say post_one.php and access the data there?

+4
source share
6 answers

If you want to save the POST data when redirecting the user (i.e. in the browser), create a new form containing the POST data (make sure you use htmlspecialchars ) and then submit it (by sending it to the new destination location).

If this is a fully server side, you can simply execute a simple POST request using cURL or file_get_contents . php.net has a lot of information about this topic.

+3
source

You must send an HTTP POST request to your URL. The option is to do this with file_get_contents and provide context. Creating context is easy with stream_context_create . An example is as follows:

 $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 send a POST request to $ url. $_POST['first'] and $_POST['second'] will be available in your destination URL.

If you want to republish all published variables, replace the first line as follows:

 $data = http_build_query($_POST); 
+15
source

Using cURL, for example. You can pass $ _POST as an array of messages.

 <?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); 
+2
source

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

  • Just add the appropriate data to the hidden field in the form and submit this form to a new location.

  • Save the relevant data in the $ _SESSSION variable and use header('Location: /...'); to redirect to a new destination. Then you can extract the data from the $ _SESSION array in the right place.

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

Of these, I would recommend the second approach, as this will prevent data from being visible to the end user.

+1
source

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

 include 'path/to/post_one.php'; 

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

But if it outputs some html or needs to be redirected to it for proper operation, then it can get hairy, don't do it like this.

+1
source

In post.php :

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

Then in post_one.php :

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

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

0
source

Source: https://habr.com/ru/post/1334961/


All Articles