我已经完成了这个question ,但他们发布的答案并没有解决我的问题。
出现的问题是,如果用户点击浏览器的后退按钮返回到已提交的表单,输入的数据会保留并且用户能够“重新提交”表单。 我怎样才能防止这种行为(laravel 的方式)?
我的 route.php 看起来像
Route::group(array('after' => 'no-cache'), function()
{
Route::get('/', 'HomeController@index');
Route::ANY('/search','HomeController@search');
Route::get('user/login',array('as'=>'user.login','uses'=>'UserController@getLogin'));
Route::post('user/login',array('as'=>'user.login.post','uses'=>'UserController@postLogin'));
Route::get('user/logout',array('as'=>'user.logout','uses'=>'UserController@getLogout'));
Route::post('user/update/{id}',array('as'=>'user.update','uses'=>'UserController@userUpdate'));
Route::group(array('before' => 'auth'), function()
{
Route::get('user/profile',array('as'=>'user.profile','uses'=>'UserController@getUserRequest'));
Route::get('order/checkout','OrderController@checkout');
Route::get('order/status',array('as'=>'order.status','uses'=>'OrderController@orderStatus'));
Route::group(array('before' => 'csrf'), function()
{
Route::post('order/process','OrderController@process');
});
});
});
过滤器.php
Route::filter('csrf', function()
{
if (Session::token() != Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
});
Route::filter('no-cache',function($route, $request, $response){
header("Cache-Control: no-cache,no-store, must-revalidate"); //HTTP 1.1
header("Pragma: no-cache"); //HTTP 1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
});
Controller 代码
public function process(){
//data is saved to database
Session::put('_token', md5(microtime()));
return Redirect::route('order.status');
}
public function orderStatus(){
return View::make('orderStatus')->with('message','done');
}
最佳答案
轮类交换:
Are you sure your browser is not 'refreshing' the page when it presses 'back' - because of 'no-cache'? Try this: load the form, view the source, look @ the hidden token code. Then submit the form, press back, and @ look at the hidden token code - are they the same?
尝试 self 拉胡:
no they aren't the same
那就是你的答案!当您按下“后退”时,您的浏览器正在“刷新”页面!
因此您的代码“适用于”大多数浏览器 - 但无论您使用哪种浏览器都会自动刷新“背面”页面 - 因此您的 token 将重新填充到表单上。就像用户正在“重新访问”表单一样 - 所以您几乎无法阻止这种情况。它适用于大多数浏览器...
或者您可以关闭表单的“无缓存” - 或者将其设置为 5 分钟或其他时间 - 这样浏览器就不会刷新页面。
也许有一个“表单”缓存过滤器 - 这是 5 分钟和所有其他站点的过滤器 - 这是 0,类似的东西将是“Laravel 优雅”:)
关于php - 拉维尔 4 : Prevent form re-submissions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17840727/