What is the question ? There is nothing particular to do.
CSRF, Ajax and security
I made a basic ajax post request and echo the posted data, nothing fancy.
Is it okay to use like that or is there anything I need to add to make it more secure? I added csrf_token() with double curly braces to meta name but it's not being displayed here (content isn't empty).
1-) I added meta CSRF.
2-) I added $.ajaxSetup to jQuery
3-) I echo the $variable.
My question is, are the steps above provide enough security for this particular Ajax/Laravel process or do I still need to add <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> inside my forms? I read a tutorial that states you can add it globally : http://tutsnare.com/post-data-using-ajax-in-laravel-5/
But I don't see any hidden field in my form when I view it in chrome dev tools.
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
<form action="" method="post">
<input id="name" type="text" placeholder="Enter your name">
<input type="submit" id="sub">
</form>
<div id="result"></div>
<script src="/js/jquery-2.1.4.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// set up jQuery with the CSRF token, or else post routes will fail
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}); // handlers
$("#sub").click(function (y) {
y.preventDefault();
var user_name = $('#name').val();
$.post(
"/settings/generalsettings/changedata",
{
name99: user_name
},
function (data) {
$('#result').hide().html(data).fadeIn(2000);
});
});
});
</script>
</body>
</html>
UserSettingsController.php
public function changeData()
{
$variable = Input::get('name99');
echo $variable;
}
Please or to participate in this conversation.