1

I know this is duplicate question. I tried all solutions from stackoverflow.com but I could not resolve it. This is very random behavior. When php curl post to destination server api, sometimes same request get posted twice to destination. I checked if source php is refreshed, but php is not refreshed. Another strange thing I noticed that, I get curl output of re-post request only. I don't get curl output of first original request.

$curl_unit = curl_init($URL);

curl_setopt($curl_unit, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl_unit, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_unit, CURLOPT_POST, 1);
curl_setopt($curl_unit, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl_unit, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_unit, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_unit, CURLOPT_INTERFACE, gethostbyname($_SERVER['HTTP_HOST']));
curl_setopt($curl_unit, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($curl_unit, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

$curl_output = curl_exec($curl_unit);
$code = curl_getinfo($curl_unit, CURLINFO_HTTP_CODE);

curl_close($curl_unit);
SaveMyLog("DATA RECEIVED FROM destination: \r\n" . $curl_output . "\r\n Http code response: " . $code . "\r\n");

Above mentioned code is general code for curl in all php pages of the project. Later I tried different curl code when I found some solutions on stackoverflow. Following is new curl code which is also not working sometimes and same random strange behavior happens.

$curl_unit = curl_init($URL);

curl_setopt($curl_unit, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl_unit, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_unit, CURLOPT_POST, 1);
curl_setopt($curl_unit, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl_unit, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($curl_unit, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_unit, CURLOPT_INTERFACE, gethostbyname($_SERVER['HTTP_HOST']));
curl_setopt($curl_unit, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($curl_unit, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

ob_start(); 
curl_exec($curl_unit);
$code = curl_getinfo($curl_unit, CURLINFO_HTTP_CODE);
curl_close($curl_unit);
$curl_output = ob_get_contents(); 
ob_end_clean();         

SaveMyLog("DATA RECEIVED FROM destination: \r\n" . $curl_output . "\r\n Http code response: " . $code . "\r\n");
ob_end_flush();

Please note $URL and $postdata is already defined. SaveMyLog function is used for logging purpose.

function SaveMyLog($lin, $deprecated = 'mylog.log')
{
    $logid = '';
    $date = getdate();
    $fileName = basename($_SERVER['SCRIPT_FILENAME']);
    $file = str_replace('.php', '', $fileName);

    $logfile = $file . "_" . $date['year'] . "-" . $date['mon'] . "-" . $date['mday'] . ".log";

    $fd = fopen('./logs/application_logs/' . $logfile, 'a+');
    fwrite($fd, date('Y-m-d H:i:s') . $logid . "\t" . $lin . "\n");
    fclose($fd);
    @chmod('../logs/application_logs/' . $logfile, 0666);
}
Sarvesh
  • 551
  • 1
  • 10
  • 25
  • 1
    How about using TOKENS (in the form for post data) to prevent send twice the form (that can call then 2 times the same url with curl)? Read this http://stackoverflow.com/questions/14753148/why-does-a-browser-send-two-requests-for-the-same-page-when-its-refreshed can be the bug. – JustOnUnderMillions Apr 20 '17 at 11:49
  • Try setting `CURLOPT_RETURNTRANSFER` to `true`. `curl_setopt($curl_unit, CURLOPT_RETURNTRANSFER, 1);` – Jay Blanchard Apr 20 '17 at 12:00
  • This may give you more information: `print curl_error($curl_unit);` - place it after the curl_exec – Graham Apr 20 '17 at 12:25
  • @JustOnUnderMillions I think TOKEN would be useful in case of refresh. But this is not case of refresh. – Sarvesh Apr 20 '17 at 12:30
  • @JayBlanchard I tried `curl_setopt($curl_unit, CURLOPT_RETURNTRANSFER, 1);` but problem still persists – Sarvesh Apr 20 '17 at 12:34

3 Answers3

0

You could try using a CSRF token, this would get changed after every request.

0

from your code i see no reason why it should behave in such strange manner.

So there can be some other problem. If you are calling the script from a browser, make sure the browser calls it just once.

Why does a browser send two requests for the same page when it's refreshed?

Community
  • 1
  • 1
Vody
  • 78
  • 7
  • Hi Vody. I have made sure that it is not related with `browser refresh`. If it is a `browser refresh` then I could see all `logs` in printed twice at `source php` where I am calling this curl to destination. However I can see same request getting printed at destination and this is random behavior. – Sarvesh Apr 20 '17 at 14:40
  • Hey, just for the sake of completeness, put log also in the beginning of the code. I had similar problem as you are experiencing and it was cause by Safari and the php script was being prematurely terminated. So if you send random numbers throught curl. Your destination will receive 2 requests under 1 random number, right? – Vody Apr 20 '17 at 15:31
  • Hi, I could give you logs but I can see logs are printed only once. In logs, I am printing postdata, url and curl output and http code. And yes, destination is receiving 2 requests under 1 random number. This behavior is random. – Sarvesh Apr 21 '17 at 06:23
0

We had the same issue and we solved it by disabling or uninstalling the Firebug Lite extension.

shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
Jose Gomez
  • 11
  • 1