1

I've been using PHP and curl to post sensitive data to my backend server. However, after the 2nd or 3rd post request, the post data doesn't seem to be clearing, instead, it appears to concatenate the old post data to the new data being sent.

Here is an example of how I am creating my request:

function Communicate($req, $postdata)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, sprintf('%s%s', $this->server_uri, $req));
    curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, substr_count($postdata, '='));
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);

    $res = curl_exec($curl);
    curl_close($curl);
    unset($postdata);
    $sfr = json_decode($res);
}

As you can see, it is fairly straightforward.

However, I am tracing the post request on my backend server and here is what I get:

1st request

Restful Function: ValidateUserByEmail Params: [email protected]&pass=foo

2nd request

Restful Function: GetRegionByUserId Params: [email protected]&pass=foo

The userid param of the 2nd request is supposed to be just 1. I can see that this is being set correctly before the curl_exec function is executed.

2 Answers 2

1

Your function seems straightforward, but assuming there is something happening on the receiving end, why not use sleep to delay the connection on each loop to give your receiving point time to catch up?

So for example, this part of your code is:

    $res = curl_exec($curl);
    curl_close($curl);
    unset($postdata);
    $sfr = json_decode($res);
}

But with sleep would be:

    $res = curl_exec($curl);
    curl_close($curl);
    unset($postdata);
    $sfr = json_decode($res);
    sleep(3);
}

The way I have that set now it will sleep for 3 seconds, but I would recommend shortening or even lengthening it to see what is the best balance of speed, performance & accuracy for your setup.

Sign up to request clarification or add additional context in comments.

1 Comment

I think you might be on to something here. I'm going to do a little bit of testing with this and get back to you.
1

If it were me, I would very carefully inspect the code that's calling Communicate(). It seems to me that if there's a bug, it's related to the handling of the $postdata argument before that arg is passed to the function. Litter that code with var_dump($postdata) and I'll bet you'll find one of those bugs that's so obvious it's impossible to find.

(The same goes for the code on the server receiving those requests.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.