Skip to content

Instantly share code, notes, and snippets.

@zero-is-one
Forked from ericlbarnes/filters.php
Created October 13, 2013 21:00
Show Gist options
  • Save zero-is-one/6967383 to your computer and use it in GitHub Desktop.
Save zero-is-one/6967383 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