CSRF TokenMismatchException Problem in Laravel 5.*
应该对一些人有帮助……
It is that the ob_start, ob_get_clean don't work synchronously in the view process causes the problem. In this circumstance, the view process echoes the content of site first anomalously before the response sends cookie, headers, and content. So the browser doesn't receive header correctly. Next time the browser sends request, the session id mismatches because lack of correct cookies, then trigger the CSRF token mismatch problem. Details are in here: https://github.com/wyl206/Web/blob/master/...
The solution is simple. That is to maintain it is in the same output buffering level after excuted "include $__path". So I change the evaluatePath function in file "Illuminate\View\Engines\PhpEngine.php". Here is my solution Code:
protected function evaluatePath($__path, $__data)
{
$obLevel = ob_get_level();
extract($__data);
ob_start();
$obStartLevel = ob_get_level();
// We'll evaluate the contents of the view inside a try/catch block so we can
// flush out any stray output that might get out before an error occurs or
// an exception is thrown. This prevents any partial views from leaking.
try {
include $__path;
} catch (Exception $e) {
$this->handleViewException($e, $obLevel);
} catch (Throwable $e) {
$this->handleViewException(new FatalThrowableError($e), $obLevel);
}
//最终是要保证obEndLevel和obStartLevel在同一层
$obEndLevel = ob_get_level();
while($obEndLevel > $obStartLevel){
ob_end_flush();
$obEndLevel = ob_get_level();
}
$myContent = ltrim(ob_get_contents());
while($obEndLevel < $obStartLevel){
ob_clean();
if(!ob_start()) break;
$obEndLevel = ob_get_level();
}
if($obEndLevel === $obStartLevel) ob_end_clean();
return $myContent;
}
推荐文章: