Skip to content

Instantly share code, notes, and snippets.

@tarex
Forked from touhidurabir/filters.php
Created August 13, 2014 07:20
Show Gist options
  • Save tarex/29951c9428433cc0a11d to your computer and use it in GitHub Desktop.
Save tarex/29951c9428433cc0a11d to your computer and use it in GitHub Desktop.
/**
* Filter to check for CSRF attacks from the ajax requests.
*/
Route::filter('csrf_header', function()
{
if (Session::token() != Request::header('x-csrf-token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
});
$.ajax({
url: 'post/add',
type: 'post',
dataType: 'json',
data: this.data,
beforeSend: function(request) {
return request.setRequestHeader("X-CSRF-Token", $("meta[name='token']").attr('content'));
},
success: function(ev) {
},
error: function(xhr, error, status) {
}
});
<meta name="token" content="{{ Session::token() }}">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment