Edit 25.07.2018: As Pawnesh Kumar said in the answer, this seems to be a browser issue. If I hit the button multiple times in Firefox the below script will only
It might be because you lock a file exclusively LOCK_EX
with flock
and then try to write to it with file_put_contents
which under the hood opens a new pointer with fopen
, and since you already locked the file exclusively it can't write to your file.
Try replacing the file_put_contents
with fwrite($fp, $current, strlen($current))
, and add an append flag to your fopen
like this fopen($file, a+)
Does this reproduce the issue of multiple writes?
UPDATE
The token exception mismatch from the video you provided happens because of the Laravel's CSRF protection.
Every form in Laravel needs to have a {{ csrf_field() }}
or you need to provide the crsf_token
in the Header for AJAX requests.
That csrf_token
in the form's hidden field has that same value stored in the Laravel's user session. When you double click submit fast on a form, the first request is sent with the first csrf_token
, when it hits Laravel the token sent in the form request and the token stored in the session are compared, if they are equal the request passes, and a new csrf_token
is generated in the session.
Since you did not reload the view the new token couldn't be rendered in the hidden form field, so by the time the second request hits we have a new csrf_token
in the session and the old one in the second form request and thus the exception is thrown.
UPDATE 2
As I said it was the flock
, try this code and the multiple submit issue will work