开发者

Is there a way to redirect a user with POST request?

I need to redirect an user, but I need to simulate some form processing, the 开发者_如何转开发end point way for a POST request, so I basicly want to redirect my user as he was sending a post request.

I know I can do a page between the both and use javascript to submit an hidden form, but I'd like to avoid that.


Im assuming that you're doing some sort of stats logging, or something, in the intermediate server?

Another way of doing it.. have the form POST to the target URL, but use ajax to grab the form data first.

<form id="formid" onsubmit="saveFormValues(this);return true" target="http://target/url">

</form>

the browser will call saveFormValues() first, and then return true indicating that the form can be processed in the normal way.

Of course, this wont work for people without javascript. The only way of POSTing to your indermediate server, is for your intermediate server to POST the data to the target server itself using cURL. And then returning the reponse back.

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://target/url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);


why not add a hidden value on your form then do this;

if (isset($_POST['submit']) {

// form is submmitted

header('location: /')

}

elseif (!isset($_POST['submit'])) {

// redirect to form

}

else {

// error handling or something else

}

You could even go further and better by creating a class and objects, by assigning the form values to them you could check for further instances.

It was hard to tell your question, but this is what I gathered if not, throw me a comment and I will edit my answer bud!

:)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜