PHP - strange issue - corrupted headers/content due to seemingly unrelated code line
I have spent 3 days & 4 开发者_如何学PythonSO questions trying to fix something which didn't have a problem in the first place. However, the actual problem now has me stumped. Please see the following curl code. It correctly fetches a particular webpage and displays. There is a commented line (no. 8), which sets an additional POST variable. If I uncomment it, then the browser tries to download a gzip file instead of displaying it. I don't know the correlation between that line and the strange behaviour at all.
(Note: I have used a static url from w3schools so others can try this code. I am trying to use this code for my own internal data server and proxy, and am facing exactly same issue. Uncommenting that particular line results in the strange behaviour. I need to use this variable. I don't presently know a work-around plus am very curious to find the cause).
<?php
session_start();
$i_var_prefix="i_var_";
// Other important client dependent 'SERVER' variables.
if(isset($_SERVER['HTTPS'])) { $_POST["${i_var_prefix}_HTTPS"]=$_SERVER['HTTPS']; };
//if(isset($_SERVER['REMOTE_ADDR'])) { $_POST["${i_var_prefix}_REMOTE_ADDR"]=$_SERVER['REMOTE_ADDR']; };
// STRANGE PROBLEM:: IF I UNCOMMENT THE LINE ABOVE, THEN THE BROWSER DOES NOT DISPLAY THE CONTENT BUT TRIES TO DOWNLOAD IT.
$curl_url="http://www.w3schools.com/TAGS/form_action.asp";
// Set values of these header variables as got from client
$field_array= array(
'Accept' => 'HTTP_ACCEPT',
'Accept-Charset' => 'HTTP_ACCEPT_CHARSET',
'Accept-Encoding' => 'HTTP_ACCEPT_ENCODING',
'Accept-Language' => 'HTTP_ACCEPT_LANGUAGE',
'Connection' => 'HTTP_CONNECTION',
'Host' => 'HTTP_HOST',
'Referer' => 'HTTP_REFERER',
'User-Agent' => 'HTTP_USER_AGENT'
);
$curl_request_headers=array();
foreach ($field_array as $key => $value) {
if(isset($_SERVER["$value"])) {
$server_value=$_SERVER["$value"];
$curl_request_headers[]="$key: $server_value";
}
};
//------
session_write_close();
//Open connection
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_COOKIE,session_name()."=".session_id().";");
//Set the url, number of POST vars, POST data
curl_setopt($curl_handle, CURLOPT_URL, $curl_url);
curl_setopt($curl_handle, CURLOPT_POST, count($_POST));
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $curl_request_headers);
//Execute post
$result = curl_exec($curl_handle);
//Close connection
curl_close($curl_handle);
list($headers,$content)=explode("\r\n\r\n",$result,2);
foreach (explode("\r\n",$headers) as $hdr) {
header($hdr);
}
echo $content;
?>
UPDATE:
With the line uncommented, the result headers received are:
HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Wed, 22 Dec 2010 14:32:43 GMT Server: Microsoft-IIS/6.0 MicrosoftOfficeWebServer: 5.0_Pub X-Powered-By: ASP.NET Content-Length: 478 Content-Type: text/html Set-Cookie: ASPSESSIONIDSASCDCDT=KIIKANGALGLDJMLFHGPJBBOM; path=/ Cache-control: private
With the line commented out, result headers are:
HTTP/1.1 200 OK Date: Wed, 22 Dec 2010 14:34:21 GMT Server: Microsoft-IIS/6.0 MicrosoftOfficeWebServer: 5.0_Pub X-Powered-By: ASP.NET Content-Length: 478 Content-Type: text/html Set-Cookie: ASPSESSIONIDSASCDCDT=JLPKANGAHDCNADBMNGHGIMCO; path=/ Cache-control: private
1) Why the difference?
2) How to handle the continue thing correctly?
SUMMARY:
The reason was that with that line, the number of POST variables increased to more than 1 and curl started automatically sending "Expect:" in header. This made the server respond with "Continue" header, which I was not handling. I am using the solution posted below. The comments, specially Mchl's comments - were very helpful in getting me in the right direction as I had no clue how that line could affect the behaviour.
regards,
JP
Found one solution through some guesswork and searching. From http://matthom.com/archive/2008/12/29/php-curl-disable-100-continue-expectation. The 100-continue status is "expected" by default when using cURL-
Expect: 100-continue
.
I disabled it using
$curl_request_headers[]="Expect: ";
.
And now it works! (I guess server stops sending the continue header). (However, I am not convinced this is robust for all types of requests that the client can make [in the long run] but might be okay for normal purpose).
Obviously uncommenting this line changes the $result
you get from curl_exec
.
You have two curl options you need to examine:
curl_setopt($curl_handle, CURLOPT_POST, count($_POST));
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $_POST);
[edit]
A quick fix might be to remove the HTTP 100 header from $headers.
Two suggestions:
- Change the offending line to
if(isset($_SERVER['REMOTE_ADDR'])) _POST['unrelated_variable'] = 42;
. If this does not help, then you can rule out any warning-related issues, leaving setting values on$_POST
as the only possible culprit. - Change your design so that you do not need to do direct writes on
$_POST
which many, myself included, consider a very bad idea.
精彩评论