简体   繁体   English

PHP 5.6.9上的Laravel 5 TokenMismatchException

[英]Laravel 5 TokenMismatchException on PHP 5.6.9

Post requests work fine running Laravel 5 app on PHP 5.4. 在PHP 5.4上发布请求可以正常运行Laravel 5应用程序。 Post requests on the same app running on PHP 5.6.9 generate: 在PHP 5.6.9上运行的同一个应用程序上发布请求会生成:

TokenMismatchException VerifyCsrfToken.php on line 46 TokenMismatchException第46行的VerifyCsrfToken.php

This happens on every post request on both WAMP and IIS. WAMP和IIS上的每个帖子请求都会发生这种情况。 Happens using database sessions and file sessions. 发生使用数据库会话和文件会话。 Did a full reinstall and also tried all suggestions made here: https://laracasts.com/discuss/channels/general-discussion/keep-getting-tokenmismatchexception-verifycsrftokenphp-on-line-46?page=2 . 完全重新安装并尝试了所有建议: https//laracasts.com/discuss/channels/general-discussion/keep-getting-tokenmismatchexception-verifycsrftokenphp-on-line-46?page=2 Folks are disabling the Csrf middleware as a fix, but that is not a viable solution. 人们正在禁用Csrf中间件作为修复,但这不是一个可行的解决方案。 Any help appreciated. 任何帮助赞赏。

When I realized this was only happening in IE and Chrome, but not Firefox, it led me to the fix. 当我意识到这只发生在IE和Chrome中,而不是Firefox时,它引发了我的修复。 The app was using AddThis share buttons and the javascript was adding an iframe to the pages. 该应用程序使用AddThis共享按钮,javascript正在向页面添加iframe。 This issue is resolved by adding a P3P header to the VerifyCsrfToken Middleware. 通过向VerifyCsrfToken中间件添加P3P标头来解决此问题。 Hope this saves somebody the hours I lost. 希望这可以节省一些人丢失的时间。

public function handle($request, Closure $next)
    {
        $response = $next($request);

        if (last(explode('\\',get_class($response))) != 'RedirectResponse') {
            $response->header('P3P', 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
        }

        return $response;
    }

put <meta name="csrf_token" content="{{ csrf_token() }}"> so token is always available and use that with your AJAX request: ex: put <meta name="csrf_token" content="{{ csrf_token() }}">因此令牌始终可用并将其与您的AJAX请求一起使用:ex:

var csrftoken =  (function() {
var metas = window.document.getElementsByTagName('meta');
for(var i=0 ; i < metas.length ; i++) {
    if ( metas[i].name === "csrf-token") {
        return  metas[i].content;
    }
}})();

just add {{ csrf_field() }} to your form block and it will save you lot of time 只需将{{ csrf_field() }}到表单块中,就可以节省大量时间

<form action="{{ route('signup') }}" method="post" class="form-signin">
{{ csrf_field() }}
...
</form>

For anyone, who is facing this issue in spite of adding {{ csrf_field() }} in the form field and even by adding this in the head tag <meta name="csrf-token" content="{{ csrf_token() }}"> 对于任何人,即使在form字段中添加{{ csrf_field() }} ,甚至在head标记中添加此内容,也面临此问题<meta name="csrf-token" content="{{ csrf_token() }}">

The issue is with writing in the storage because of not adding your current user on Linux in the right group. 问题在于在存储中写入,因为没有在正确的组中在Linux上添加当前用户。 Make sure to add your current user to www-data group. 确保将当前用户添加到www-data组。 Write this command 写下这个命令

sudo chown -R :www-data /path/to/laravel-folder

Make sure to give right permission to storage folder as well in the laravel app. 确保在laravel应用程序中为存储文件夹提供正确的权限。

sudo chmod -R 775 /path/to/laravel-folder/storage

I hope this helps. 我希望这有帮助。

I have faced the same issue and I could solve it by just adding the following package on top: "Spatie\\Permission\\PermissionServiceProvider::class," 我遇到了同样的问题,我可以通过在顶部添加以下包来解决它: “Spatie \\ Permission \\ PermissionServiceProvider :: class,”

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM