我在 Laravel 5.8 中创建了一个项目。在我的本地环境 (PHP 7.2) 中,它运行良好。当我在登录后使用 cpanel 将这个项目托管到我的服务器(PHP 7.1)时,它返回 419 页面过期错误。
我的登录表单代码:
<form method="POST" action="{{ route('login') }}" id="login-form">
@csrf
<div class="form-group">
<label for="username">{{ __('Username / Email Address') }}</label>
<input type="text" class="form-control{{ $errors->has('username') ? ' is-invalid' : '' }} boxed" name="username" id="username" value="{{ old('username') }}" required autofocus>
</div>
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
<div class="form-group">
<label for="password">{{ __('Password') }}</label>
<input type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }} boxed" name="password" id="password" required>
</div>
@if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
<div class="form-group" style="margin-bottom: 0px; float:left;">
@if (Route::has('password.request'))
<a href="{{ route('password.request') }}" class="forgetpwd">
{{ __('Forgot Your Password?') }}
</a>
@endif
</div>
<div class="form-group" style="text-align: center;">
<button type="submit" class="btn btn-warning" style="padding:0.5rem 1.8rem;">Login</button>
</div>
</form>
我清除了缓存和 Cookie,但是,显示了同样的问题。
此错误是由于 CSRF 令牌验证失败、配置错误的缓存、权限、不正确的会话设置引起的。当用户提交发布请求时会出现此错误。您可以通过执行以下操作来修复它:
CSRF 令牌验证失败 419 错误最常见的原因是 CSRF 令牌失败。跨站点请求伪造是服务器生成的唯一加密值。这包含在客户端的 HTTP 请求中。稍后服务器验证它。如果失败,则会导致会话过期错误。因此,您检查 Laravel 配置中的 CSRF 设置。
缓存导致的会话过期错误 缓存有时也会导致前端出现会话过期错误。这可以是服务器缓存和浏览器缓存。所以,使用
php artisan cache:clear
清除服务器缓存。Laravel 文件和文件夹权限 同样,不当的文件或文件夹权限也会导致错误。通常,Web 服务器需要 Laravel 文件夹存储和供应商的写入权限。此外,会话存储需要写权限。所以,给予权限,
chmod -R 755 存储
chmod -R 755 供应商
chmod -R 644 引导程序/缓存
Laravel 会话设置 最后但并非最不重要的一点是,会话设置也会导致 419 错误。 app/config/session.php 是会话配置文件。检查一些重要参数——域和安全。
'domain' => env('SESSION_DOMAIN', null),
'secure' => env('SESSION_SECURE_COOKIE', false), // in case of cookie
这些逐步的方法修复了错误并使 Laravel 再次工作。
很可能,你错过了@csrf.
只需在表单打开标记行之后添加 @csrf。 它应该是这样的:
<form class="singn-form" method="POST" action="{{ route('register') }}">
@csrf
....
</form>
就我而言
当您登录并想再次注册时,此错误被触发。
所以我在新的隐身标签中打开并解决了我的问题
更新:Asif 在评论中说:
或者您可以清除浏览器 cookie,因为 laravel 在那里使用会话。
在 head 部分使用它而不是 @csrf :
<meta name="csrf-token" content="{{ csrf_token() }}">
是的,问题出在csrf_token上。 @csrf 仅返回令牌但将要发送,因此使用 csrf_field() 将生成一个隐藏的输入字段。或者您可以像下面这样从中间件中删除此路由,不推荐这样做,因为它是您的身份验证路由。另外,试试
清除缓存:
php artisan cache:clear
生成新的应用程序密钥:php artisan key:generate
class VerifyCsrfToken extends Middleware
{
/**
* Indicates whether the XSRF-TOKEN cookie should be set on the response.
*
* @var bool
*/
protected $addHttpCookie = true;
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'/login'
];
}
在您的 .env 文件中添加
SESSION_DOMAIN=
而不分配任何值。
如果它是共享主机..,只需在顶部添加index.php
ob_start();
另一件需要注意的事情是,如果您通过 http(非安全)访问该站点,但您在
config/session.php
中启用了“仅 HTTPS Cookie”,您将收到此错误。
/*
|--------------------------------------------------------------------------
| HTTPS Only Cookies
|--------------------------------------------------------------------------
|
| By setting this option to true, session cookies will only be sent back
| to the server if the browser has a HTTPS connection. This will keep
| the cookie from being sent to you if it can not be done securely.
|
*/
'secure' => true,
如果以上解决方案都不起作用,请运行这些命令
php artisan key:generate
php artisan config:cache
php artisan cache:clear
php artisan view:clear
php artisan route:clear
参考这个答案这里
正如其他人所说,这应该是 csrf_token 问题。但是,就我而言,我还找不到合适的解决方案,所以我暂时从中间件中删除了所有路由以测试其余功能。
PS:- 不要在生产中这样做。
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'*',
];
}
我解决了session作为数据库的session问题(419页Expire)。我认为这会有所帮助。
将 .env 变量更改为
SESSION_DRIVER=database
SESSION_LIFETIME=120
运行这个命令
php artisan session:table
php artisan cache:clear
php artisan migrate
我有同样的问题,我朋友的解决方案都不适合我。 然后我意识到因为我的站点是https并且我使用http登录,所以我收到这个错误,所以在意识到这一点之后,我将http发送到https。
做完
php artisan key:generate
我总是得到419。
因为我的session属于之前的APP_KEY。 我清除了浏览器缓存,这一切都对我有用
在存储/框架中,文件夹“会话”对我来说丢失了。这是导致我遇到此错误的问题。创建文件夹解决了它。
请检查您的表格中必须有@csrf 令牌。
如果您在该页面中使用此指令,则必须将其删除。
我之前遇到过这个问题(Laravel 5x),去掉后就解决了
$request->session()->flush();
此指令使 csrf = empty.
在