防止重新提交表单数据 Laravel 5.5
Preventing resubmission of form data Laravel 5.5
我正在研究 Laravel 5.5 框架。
我有一个这样的表单主页:
<form action = "/result" method = "post">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token() ?>">
<table>
<tr>
<td>Name or Nickname</td>
<td><input type = "text" name = "name_nickname" autofocus /></td>
</tr>
<tr>
<input type = "submit" value = "LuckyNumber" />
</td>
</tr>
</table>
控制器看起来像这样:
class SixGetController extends Controller
{
public function luckyNumber(Request $request){
$nameNickname = $request->input('name_nickname');
$luckyNumber = rand (1,10);
DB::table('visitor')->insert(
['name_nickname' => $nameNickname, 'luckyNumber' => $luckyNumber]);
return view('result', ['nameNickname' => $nameNickname, 'luckyNumber' =>
$luckyNumber]);
}
结果页面如下所示:
<p><?php echo $nameNickname; ?> </p>
<p>Your lucky number is <?=$result?> .</p>
如果用户按下重新加载 F5 按钮,脚本将重新滚动随机数生成器并使用重新滚动的数字重新提交数据。我已经阅读了我不知道如何使用的 PGR 模式以及我也不了解的有关操纵历史的内容。有人可以指出我在某处放置了什么样的代码以防止重新滚动和重新提交。谢谢
对于laravel实现,您可以使用Session Flash Data。
Sometimes you may wish to store items in the session only for the next
request. You may do so using the flash method. Data stored in the
session using this method will only be available during the subsequent
HTTP request, and then will be deleted. Flash data is primarily useful
for short-lived status messages:
在这种情况下,当有人发出 post 请求时,您应该将有用的数据存储到会话中,并将它们重定向到其他路由。然后另一个路由可以检索有用的数据以显示到视图,并且用户在刷新页面时不再重新提交表单。
public function luckyNumber(Request $request) {
...
$request->session()->flash('nameNickname', $nameNickname);
$request->session()->flash('luckyNumber', $luckyNumber);
return redirect()->action('SixGetController@resultView');
}
public function resultView(Request $request) {
$nameNickname = $request->session()->get('nameNickname');
$luckyNumber = $request->session()->get('luckyNumber');
return view('result', ['nameNickname' => $nameNickname, 'luckyNumber' => $luckyNumber]);
}
我正在研究 Laravel 5.5 框架。 我有一个这样的表单主页:
<form action = "/result" method = "post">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token() ?>">
<table>
<tr>
<td>Name or Nickname</td>
<td><input type = "text" name = "name_nickname" autofocus /></td>
</tr>
<tr>
<input type = "submit" value = "LuckyNumber" />
</td>
</tr>
</table>
控制器看起来像这样:
class SixGetController extends Controller
{
public function luckyNumber(Request $request){
$nameNickname = $request->input('name_nickname');
$luckyNumber = rand (1,10);
DB::table('visitor')->insert(
['name_nickname' => $nameNickname, 'luckyNumber' => $luckyNumber]);
return view('result', ['nameNickname' => $nameNickname, 'luckyNumber' =>
$luckyNumber]);
}
结果页面如下所示:
<p><?php echo $nameNickname; ?> </p>
<p>Your lucky number is <?=$result?> .</p>
如果用户按下重新加载 F5 按钮,脚本将重新滚动随机数生成器并使用重新滚动的数字重新提交数据。我已经阅读了我不知道如何使用的 PGR 模式以及我也不了解的有关操纵历史的内容。有人可以指出我在某处放置了什么样的代码以防止重新滚动和重新提交。谢谢
对于laravel实现,您可以使用Session Flash Data。
Sometimes you may wish to store items in the session only for the next request. You may do so using the flash method. Data stored in the session using this method will only be available during the subsequent HTTP request, and then will be deleted. Flash data is primarily useful for short-lived status messages:
在这种情况下,当有人发出 post 请求时,您应该将有用的数据存储到会话中,并将它们重定向到其他路由。然后另一个路由可以检索有用的数据以显示到视图,并且用户在刷新页面时不再重新提交表单。
public function luckyNumber(Request $request) {
...
$request->session()->flash('nameNickname', $nameNickname);
$request->session()->flash('luckyNumber', $luckyNumber);
return redirect()->action('SixGetController@resultView');
}
public function resultView(Request $request) {
$nameNickname = $request->session()->get('nameNickname');
$luckyNumber = $request->session()->get('luckyNumber');
return view('result', ['nameNickname' => $nameNickname, 'luckyNumber' => $luckyNumber]);
}