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;
}
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!