我可以在PHP中发布数据然后使用Header命令吗?

我可以在PHP中发布数据然后使用Header命令吗?

问题描述:

$bEmail=mysql_real_escape_string($_POST['bEmail']);
$bPassword=mysql_real_escape_string($_POST['bPassword']);
/webapp/wcs/stores/servlet/Do/LogonForm
header("Location: /cart/payment");

I want to POST the form /webapp/wcs/stores/servlet/Do/LogonForm in jQuery you can do:

$.post('/webapp/wcs/stores/servlet/Do/LogonForm', {
email: <?=$bEmail?>,
password: <?=$bPassword?>
},
window.location="cart/payment"

I want to do this in PHP, so PHP runs a script, this page sends a POST back and whilst the script is running at the end it uses the Header to redirect.

Yes you can ....

$bEmail=mysql_real_escape_string($_POST['bEmail']);
$bPassword=mysql_real_escape_string($_POST['bPassword']);

//set POST variables
$url = 'http://domain.com//webapp/wcs/stores/servlet/Do/LogonForm';
$fields = array(                
            'bEmail'=>urlencode($bEmail),
            'bPassword'=>urlencode($bPassword)
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

and then redirect

header("Location: /cart/payment");
exit();  

You can call the header function whenever you would like as long as no output has been sent to the browser (i.e no HTML source or echo's can come before the header call).