代码为307的PHP重定向将方法从POST更改为GET [英] PHP redirection with code 307 changes method from POST to GET

查看:881
本文介绍了代码为307的PHP重定向将方法从POST更改为GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的PHP软件中,我具有自我更新功能,该功能会将带有POST方法的HTTP请求发送到某个URL(到PHP脚本).现在此URL已更改(我将脚本移到了另一个目录),但是为了保持向后兼容,我想使用旧URL上的脚本将POST请求重定向到新位置.我尝试使用HTTP 307状态代码,但是PHP第二次发出请求时,它将方法从POST更改为GET,尽管它不能这样做(至少我是307代码所针对的).我在Windows 7上使用PHP 5.4.29作为Apache(2.2.27)模块,并且嗅探流量以确保在请求和响应中使用HTTP 1.1.

In my PHP software I have an self-update feature which sends a HTTP request with the POST method to a certain URL (to a PHP script). Now this URL has changed (I moved the script to another directory), but to stay backward compatible I want to use the script at the old URL to redirect the POST request to the new location. I tried to use the HTTP 307 status code but the second time PHP makes the request, it changes the method from POST to GET, although it must not do this (at least I though this is what the 307 code is for). I use PHP 5.4.29 on Windows 7 as Apache (2.2.27) module and I sniffed the traffic to make sure that HTTP 1.1 is used in the request and the response.

这是我发出POST请求的方式:

This is how I make a POST request:

<?php

$requestData = http_build_query(
    array(
        "param1" => "value1",
        // and so on...
    )
);

$requestOptions = array("http"=>
    array
    (
        "protocol_version"=>"1.1",
        "method"=>"POST",
        "header"=>array(
            "Content-type: application/x-www-form-urlencoded",
            "Connection: close",
        ),
        "content"=>$requestData,
    )
);

$requestContext = stream_context_create($requestOptions);

$serverResponse = @file_get_contents("http://localhost/old/long/path/update.php", false, $requestContext);

?>

我尝试通过PHP手动和自动重定向:

I tried to redirect manually and automatically by PHP:

<?php

    // Redirect manually
    header("HTTP/1.1 307 Temporary Redirect");
    header("Location: http://localhost/update.php");

    // or redirect automatically
    header("Location: http://localhost/update.php", true, 307);

?>

根据嗅探到的数据,一切看起来都很正常.请求和响应中使用HTTP 1.1,并且使用代码307.但是PHP第二次发送请求(到仍然使用HTTP 1.1的新位置,..)时,它只是将方法更改为GET,而我的POST有效负载丢失了.

According to the sniffed data, everything looks normal. HTTP 1.1 is used in request and response and code 307 is used. But the second time PHP sends the request (to the new location, still with HTTP 1.1, ..) it simply changes the method to GET and my POST payload is lost.

同样:这不是用户/浏览器重定向-我重定向PHP.我自己提出请求,并通过软件手动进行设置,并且希望将其重定向到新位置.这与安全性相关.

Again: This is not a user / browser redirection - I redirect PHP. I make my request myself and manually though my software and I want to redirect it to a new location. This has nothing to do with a security related topic.

推荐答案

file_get_contents似乎不重新发布数据,可能是由于@daiscog突出显示的原因.

It looks like file_get_contents does not repost the data, possibly for the reason highlighted by @daiscog.

但是,URL将重新发布到重定向的URL:

Curl however will repost to the redirected url:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://localhost/old/long/path/update.php');   
curl_setopt($ch, CURLOPT_POST, true);    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData);

$serverResponse = curl_exec($ch);

不过,要么在服务器级别处理此问题(例如,重写Apache url),要么将新文件简单地包含在旧文件中会更有意义:

However it would make more sense to either handle this at the server level (eg an Apache url rewrite) or to simply include the new file in the old one:

//old.php
include('path/to/new.php');

这篇关于代码为307的PHP重定向将方法从POST更改为GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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