0

what I am doing is to write a PHP script which get's the user's email and the content they want to send and then use curl_exec to initiate a session and send to the server, and the server will do the rest based on which email service provider is associated.

But recently for one of our client, when they are submitting the request(forgot password or so), sometimes they received two automated emails(it is so weird that the duplicate email will only be triggered when it's their first time submitting the request of that day on that PC, after they triggered it once, no matter how many times they submit the request, no duplicate email at all).

I have also consulted the email service provider(Mandrill), and apparently they said they received two API call. And I have tested with a client. I have realized that after the user clicked (lets say) forgot password, only one rest call was sent to the php server. And Some how our database received two rest calls.

And I have tried really hard to debug it, but I could not solve it. So now I am trying to implement something which can stop the duplicate emails.

I am trying to make sure the following PHP script only run once within a very short amount of time(0.5 - 1 sec):

function post_json_and_decode ($url,$username=null,$password=null,$data=null)
{
    $ch = curl_init($url);
    $urldata = "{}";

    if ($data === null)
        $urldata = '{}';
    else
        $urldata = json_encode($data, true);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $urldata);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($urldata),
            $this->create_basic_authorization($username, $password)
        )
    );

    $result = curl_exec($ch);
    curl_close($ch);
    return json_decode($result,true);

}

and I wonder if anyone can shred me some light this? as I have already searched a lot, and CRON doesn't seem to be the solution for me.

Tony
  • 131
  • 1
  • 1
  • 3
  • you could log when it runs and check that before running again, but i dont think its the real solution here –  Aug 14 '17 at 21:29
  • Most servers allow you to configure rate limiting. Do it there rather than in PHP. – Barmar Aug 14 '17 at 21:48
  • Posible duplicate of: https://stackoverflow.com/questions/15626868/prevent-double-form-submit-using-tokens – Gonzalo Aug 14 '17 at 21:55
  • @Gonzalo, I have worked with clients multiple times, and each time the duplicate email is triggered( there's always one rest call generated to the PHP server, or in short, it's not caused by double clicking the button or triple click the button). Just somewhere between "After the rest call is sent to PHP server" and "curl_exec initiate a session and send to database", it generated two API call to the email service provider. – Tony Aug 15 '17 at 14:16

0 Answers0