HTTP Header Post

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
david64
Forum Commoner
Posts: 53
Joined: Sat May 02, 2009 8:12 am
Location: Wales

HTTP Header Post

Post by david64 »

Does anyone know how to redirect a page with POST variables using the header function?

I have found a few references to methods like this:

Code: Select all

<?php
$host = "www.example.com";
$path = "/path/to/script.php";
$data = "data1=value1&data2=value2";
$data = urlencode($data);
 
header("POST $path HTTP/1.1\r\n" );
header("Host: $host\r\n" );
header("Content-type: application/x-www-form-urlencoded\r\n" );
header("Content-length: " . strlen($data) . "\r\n" );
header("Connection: close\r\n\r\n" );
header($data);
?>
but all I am getting is my browser telling me to download a file.

If anyone could try running that on their system I would be interested to know what the response is. There may be some system issues.
Last edited by Benjamin on Sat May 16, 2009 6:19 pm, edited 1 time in total.
Reason: Changed code type from text to php.
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: HTTP Header Post

Post by ldougherty »

I copied your code to my server, got the same result with a popup asking me to save the PHP file.

This may be helpful..

Using PHP, how do I send a http POST without using a form?
http://www.faqts.com/knowledge_base/vie ... 039/fid/51
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: HTTP Header Post

Post by Benjamin »

Use cURL. Here's an example I commented for you.

Code: Select all

 
<?php
 
# set the post fields and values
$data = array(
    'foo' => 'bar',
    'bar' => 'foo',
);
 
# create the post string
$post_fields = '';
foreach ($data as $k => $v) $post_fields .= "$k=" . urlencode($v) . '&';
 
# initialize curl
$ch = curl_init('http://example.com/page.php');
 
# don't return the header
curl_setopt($ch, CURLOPT_HEADER, 0);
 
# return the resulting data rather than displaying it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
# tell curl we are posting data
curl_setopt($ch, CURLOPT_POST, 1);
 
# set the post data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
 
# don't validate ssl certs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 
# execute and get the response
$response = curl_exec($ch);
 
# close curl
curl_close($ch);
 
# echo response
echo $response;
 
See also: http://us3.php.net/manual/en/book.curl.php
david64
Forum Commoner
Posts: 53
Joined: Sat May 02, 2009 8:12 am
Location: Wales

Re: HTTP Header Post

Post by david64 »

Thanks ldougherty and astions. However, I am very familiar with cURL. I don't want to download the actual page and save it as a variable. I want to forward the browser to the actual page, just like I was doing:

Code: Select all

<?php
header( 'Location: http://php.net/' );
?>
However, I also need to get a few POST variables in the mix.
Last edited by Benjamin on Mon May 18, 2009 9:51 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: HTTP Header Post

Post by Darhazer »

The headers are what you send to the browser, not to the page you are going to redirect to.
I don't know other way to redirect with POST then to display a form and to immediately submit it
Post Reply