Skip to content

Instantly share code, notes, and snippets.

@iClosedz
Forked from ericlbarnes/filters.php
Created July 26, 2014 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iClosedz/f4ee0a91caed8cab0780 to your computer and use it in GitHub Desktop.
Save iClosedz/f4ee0a91caed8cab0780 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