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 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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