我有一个createOrder请求,目前有点慢(大约3-4秒)。我试图防止使用CSRF令牌或自定义令牌提交重复表单。
在这种情况下,我使用的是一个文件驱动程序.因此,这很可能是问题所在,但我还没有将这些代码推入生产服务器,因为它不是按照预期工作的。
它的工作方式如下:
"custom_token"字段中的会话和提交表单中。。
if(! hash_equals($request->session()->get('custom_token'), $request->custom_token)){
flash()->error('Order placed twice');
return redirect('/checkout/thank-you');
}
$request->session()->put('custom_token', Str::random(40));如果我将主create调用从控制器(即慢位)中删除,那么上面的内容就可以了,但是包含这个部分,hash_equals总是返回true。
所以我的问题是:
如果是这样的话,会话文件driver?
发布于 2019-10-31 14:49:08
有几件事,首先我要弄清楚为什么你的createOrder花了这么长时间,3到4秒是很长的时间。使用队列(https://laravel.com/docs/5.8/queues)并处理其中的大部分工作。
如果将您的解决方案放置在控制器的开头,您的解决方案应该工作得很好。我可能很厚,但您想要拒绝散列匹配的那些,所以您需要删除!
即
if(hash_equals($request->session()->get('custom_token'), $request->custom_token)){
flash()->error('Order placed twice');
return redirect('/checkout/thank-you');
}
$request->session()->put('custom_token', Str::random(40));我假设您正在创建您的散列,存储它并将其传递给您的表单,并且它正在正确地返回到$request中的控制器?尝试将custom_token设置为类似于“fred_bloggs”的内容,只需确保它是正确的。
发布于 2019-10-31 13:07:07
尝试在控制器代码中使用具有以下Try捕获的事务:
try{
\DB::beginTransaction(); //initiate the transaction and lock the database or table
//execute your desired code here
\DB::commit(); //commit the changes and unlock the database or table
}catch(\Exception $e){
\DB::rollback(); //revert to original state if something goes wrong
}https://stackoverflow.com/questions/58643960
复制相似问题