Today we will see how Laravel makes it easy to protect your application from cross-site request forgery (CSRF) attacks.
CSRF are a type of malicious exploit whereby unauthorized commands are performed on behalf of an authenticated user.
Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.
Anytime you define a HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware will be able to validate the request.
You can generate a hidden input field _token containing the CSRF token in the following two possible ways,
Method 1
The csrf_field
helper function generates the following HTML:
<?php echo csrf_field(); ?>
or you can use the blade syntax as follows,
{{ csrf_field() }}
Method 2
Define a HTML hidden field containing CSRF Token manually, hidden field must be name as _token
and use the csrf_token();
helper methods to generate the token as in the example bellow,
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
CSRF Token Verification
we do not need to manually verify the CSRF token on POST, PUT, or DELETE requests. The VerifyCsrfToken
middleware, which is included in the web middleware group, will automatically verify that the token in the request input matches the token stored in the session.
Be the first one to write a response :(
{{ reply.member.name }} - {{ reply.created_at_human_readable }}