Kumar V
Kumar V

Reputation: 8838

Post values in PHP Headers

I want send some data to a remote webpage from my site. Actually it can be achieved through form hidden variables. but for security reason, i want set as post variables in header and then send to that webpage. i use this code

$post_data = 'var1=123&var2=456';

$content_length = strlen($post_data);

header('POST http://localhost/testing/test.php HTTP/1.1');

header('Host: localhost');

header('Connection: close');

header('Content-type: application/x-www-form-urlencoded');

header('Content-length: ' . $content_length); 

header($post_data);

but my code doesn't work properly. help me...

Upvotes: 2

Views: 5486

Answers (4)

Benjamin Lasdinat
Benjamin Lasdinat

Reputation: 11

The only way to redirect POST Header is an HTTP 1.1 Redirect with 307/308

307 Temporary Redirect

header('HTTP/1.1 307 Redirect');
header('Location: target.php', false, 307);

308 Permanent Redirect

header('HTTP/1.1 308 Redirect');
header('Location: target.php', false, 308);

!!! Only works in PHP if the Redirect Code is setted 2 times in this way !!! If you only do the 2nd line. PHP made a normal 302 redirect.

Client get a message from browser if to accept this redirect, because of security reasons.

Upvotes: 1

Quentin
Quentin

Reputation: 943579

POST data forms the body of an HTTP request, it isn't a header, and the header method is used in making HTTP responses.

askapache has an example of making a POST request from PHP using the curl library.

Upvotes: 2

Chris Henry
Chris Henry

Reputation: 12010

If you're trying to post a request to remote server, you'll need to use a tool like cUrl. Here's an exmple:

// Create a curl handle to a non-existing location
$ch = curl_init('http://localhost/testing/test.php');

// Set options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute
$page = curl_exec($ch); 

Alternatively, if you really want to put data in the headers, you can set custom headers using something like this:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('field: value', 'field2: value') );

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157896

You're trying to put request headers into answer. Client just don't know what to do with it.

What you are trying to achieve is impossible.

What is the task you're trying to accomplish? There can be another way.

Upvotes: 2

Related Questions