jquery - CSRF Token simple way to add to forms in PHP -


i have developed project quite big, thinking in improving security saving token in session variable , sending token in each form check if correct prevent csrf attacks.

the thing project has many forms , painful , take lot of time go through each form in order add token in hidden input.

so asking myself, there easy way add hidden value each form without having go through each form? maybe using jquery, localize each form inside page add hidden input, add general $_post/$_get function check request if token correct.

this idea, there may simple , better way. there simple, fast , decent way so? best approach in situation (preparing csrf attack prevention after project has been developed). far know best way prevent csr attacks using token variables, there maybe decent way without having use token , go through each form?

see here: automatically insert csrf token in forms jquery.

you add code function, inline in <script> tag demonstrated:

jquery(document).ready(function() {   jquery("form").each(function() {     var tokenelement = jquery(document.createelement('input'));     tokenelement.attr('type', 'hidden');     tokenelement.attr('name', 'token');     tokenelement.val(<?= $token ?>);     jquery(this).append(tokenelement);   }); }); 

this automatically add server side $token included hidden form field each <form> element.

if want achieve without server side processing, added benefit above code included in .js file, store token in cookie , access value client side (using cookie plugin):

tokenelement.val($.cookie("csrftoken")); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -