重定向POST请求并保留数据。可能? [英] Redirect POST request and keep data. Possible?

查看:1502
本文介绍了重定向POST请求并保留数据。可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单。我需要上传一个文件直接到正确的服务器(目前工作量很低)。

因此我这样做:

 <?php 
$ server = file_get_contents('http://my-api.com/upload-server.php'); //返回url
?>

然后我打印我的表单,如

< form method =postaction =<?php echo $ server;?>... 

现在我想把这个步骤转移到上传开始时,像这样:

 < form method =postaction =http://my-api.com/upload-gateway.php... 

这个网址应该重定向到真实的服务器。



这样上传页面不会缓慢加载,我有静态HTML代码,我可以缓存,嵌入等...

问题是,这个工程perfetly很好罚款与获取请求,但没有发布的请求。

使用位置标题进行重定向时,请求似乎变成了获取请求。所有的帖子数据都丢失了。

这是不可能的,还是我做错了?是的,我考虑了一个远程动态JavaScript,首先打印正确的服务器的HTML代码。我宁愿不要那样做...

有什么想法吗? Maby替代uplod技术?

编辑:

这是我使用的确切html代码:

 < form method ='post'enctype ='multipart / form-data'action ='http://storage.ivana.2x。到/ rest.php动作=目标'>?; 
< input type = hidden name = testfield value =test>
要上传的文件:< input type = file name = upfile>< br>
关于文件的注意事项:< input type = text name = note>< br>
< br>
< input type = submit value =按>上传文件!
< / form>

这是我使用的重定向代码:

< pre $ if $($ _ GET [action] ==target){
header(Location:http://storage.ivana.2x.to/rest。 ?PHP的行动=测试);



$ b

这是我用来查看结果的输出代码:


$ b $

  if($ _ GET [action] ==test){
echo< pre>;
var_dump($ _ POST);
var_dump($ _ GET);
var_dump($ _ FILES);
}

上传小文件的结果如下所示:

  array(0){
}
array(1){
[action] =>
string(4)test
}
array(0){
}


解决方案

如果您真的想通过代码进行负载均衡,而可能使用上传表单缓存页面,请先选择默认的下载服务器(url);然后,onSubmit调用服务器,并找到最好的上传目标,并相应地调整action属性。

使用这种方法,不激活JS的用户仍然可以得到他们想要的,启用了JS的用户可以获得更好的上传目标,并且仍然可以缓存。此外,缓存请求的时间可能会更具投机性,因为URL请求将在实际上传之前很快发生。

唯一的麻烦是获取网址,比上面描述的过程更容易调整(我想象)。通过一个头文件指令和/或cURL调用两次上传一个文件对于缓存一个单独的html文件IMO来说似乎不是一个好的折衷。但是我不知道你在干什么。



如果您不想大量管理服务器环境并引入负载平衡,那就是选项我会建议。

请注意,我也不是交易的服务器管理员。


My problem is simple. I need to upload a file directly to the correct server (which has currently low workload).

Therefore I do:

<?php
$server = file_get_contents('http://my-api.com/upload-server.php'); // returns url
?>

then i print my form like

<form method="post" action="<?php echo $server; ?>"...

Now I would like to shift this step to when the upload starts, like so:

<form method="post" action="http://my-api.com/upload-gateway.php"...

this url should do a redirect to the "real" server.

So that the upload page doesn't slow down loading and I have static html code that I can cache, embed etc...

Problem is that this works perfetly fine with get requests but not with post requests.

The request seems like to get transformed into a get request when redirecting using the location header. All post data is lost.

Is this impossible or am I doing it wrong? And yes, I considered a remote dynamic javascript that prints the html code with the correct server in the first place. I would rather like not to do that...

any ideas? Maby alternative uplod techniques?

edit:

this is the exact html code i use:

<form method='post' enctype='multipart/form-data' action='http://storage.ivana.2x.to/rest.php?action=target'> 
<input type=hidden name=testfield value="test">
File to upload: <input type=file name=upfile><br> 
Notes about the file: <input type=text name=note><br> 
<br> 
<input type=submit value=Press> to upload the file!
</form> 

this is the redirect code i use:

if($_GET["action"] == "target") {
    header("Location: http://storage.ivana.2x.to/rest.php?action=test");
}

this is the output code i use to see the results:

if($_GET["action"] == "test") {
    echo "<pre>";
    var_dump($_POST);
    var_dump($_GET);
    var_dump($_FILES);
}

the result when uploading a small file looks like:

array(0) {
}
array(1) {
  ["action"]=>
  string(4) "test"
}
array(0) {
}

解决方案

If you really want to load balance through the code while potentially caching the page with the upload form, first select the default download server (url); then, onSubmit call the server and find the best upload target and adjust the action attribute accordingly.

With this method, users who do not activate JS still get what they want, users with JS enabled get the better upload target, and you can still cache. Additionally, the timing of the cache request could potentially be more opportunistic, since the URL request will occur very shortly before the actual upload.

The only hitch will be the call to get the URL, which you can more easily performance tune (I imagine) than the process you are describing above. Uploading a file twice through a header directive and/or cURL call don't seem like a good tradeoff for caching a single html file, IMO. But I don't know what you're up against, either.

If you don't want to heavily administer the server environment and introduce load balancing, this is the option I would suggest.

Note, also, I am not a server administrator by trade.

这篇关于重定向POST请求并保留数据。可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆