Laravel 未发送身份验证会话 cookie
Laravel auth session cookie not sending
所以我有一个非常基本的 Laravel 登录脚本
$email = Input::get('email');
$password = Input::get('password');
if(Auth::attempt(array('Email'=>$email, 'password'=>$password), true)){
return Response::json(array('success' => true,'logged_in'=>Auth::check() ));
}
我正在使用有效的电子邮件和密码登录。我将 "logged_in" 放在响应数组中作为测试,尝试后它 returns 为真。但是,"laravel_session" cookie 不会在响应中返回,后续调用也不会看到用户已登录。这只发生在我的生产服务器上,我的本地环境和测试环境都没有问题。
另外,我注意到 Response::json 正在返回 text/html MIME 类型,而不是 application/json。我不确定是什么原因造成的,所以我只是将 jQuery.ajax 数据类型设置为 "json" 来绕过它。这也只发生在我的生产服务器上。我对此完全没有想法,非常感谢您的帮助。
可能,您的控制器运行良好并设置了 cookie truely.But您的浏览器无法保存您的 cookie。由于 headers 域限制。
你确定你的 cookie 系统工作正常吗?你能检查一下 app/config/session.php 域字段是这样的吗。
'domain' => null,
尝试删除第二个布尔参数 (Auth::check(array, boolean)) 或检查您的用户 table 是否有 remember_token 列。
If you would like to provide "remember me" functionality in your application, you may pass true as the second argument to the attempt method, which will keep the user authenticated indefinitely (or until they manually logout). Of course, your users table must include the string remember_token column, which will be used to store the "remember me" token.
此外,您的代码有缺陷(打错了)或者您的数据库结构很奇怪,因为电子邮件字段是用大写字母书写的。
array('Email'=>$email, 'password'=>$password)
至
array('email'=>$email, 'password'=>$password)
在我的一个配置文件的深处,有人(可能是我)在 PHP 文件的 <?
开头之前插入了 space。这是在设置任何 headers 之前,因此不允许更改它们(意味着不能设置 cookie),因为数据已经写入输出流。因此,如果您正在寻找发生这种情况的原因,请确保您正在寻找。
由于我遇到了类似的问题,所以还有一个问题可能是:
您忘记使用“web”中间件组。
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
// ... and more
],
如果不这样做,则永远不会设置排队的 cookie。
所以我有一个非常基本的 Laravel 登录脚本
$email = Input::get('email');
$password = Input::get('password');
if(Auth::attempt(array('Email'=>$email, 'password'=>$password), true)){
return Response::json(array('success' => true,'logged_in'=>Auth::check() ));
}
我正在使用有效的电子邮件和密码登录。我将 "logged_in" 放在响应数组中作为测试,尝试后它 returns 为真。但是,"laravel_session" cookie 不会在响应中返回,后续调用也不会看到用户已登录。这只发生在我的生产服务器上,我的本地环境和测试环境都没有问题。
另外,我注意到 Response::json 正在返回 text/html MIME 类型,而不是 application/json。我不确定是什么原因造成的,所以我只是将 jQuery.ajax 数据类型设置为 "json" 来绕过它。这也只发生在我的生产服务器上。我对此完全没有想法,非常感谢您的帮助。
可能,您的控制器运行良好并设置了 cookie truely.But您的浏览器无法保存您的 cookie。由于 headers 域限制。
你确定你的 cookie 系统工作正常吗?你能检查一下 app/config/session.php 域字段是这样的吗。
'domain' => null,
尝试删除第二个布尔参数 (Auth::check(array, boolean)) 或检查您的用户 table 是否有 remember_token 列。
If you would like to provide "remember me" functionality in your application, you may pass true as the second argument to the attempt method, which will keep the user authenticated indefinitely (or until they manually logout). Of course, your users table must include the string remember_token column, which will be used to store the "remember me" token.
此外,您的代码有缺陷(打错了)或者您的数据库结构很奇怪,因为电子邮件字段是用大写字母书写的。
array('Email'=>$email, 'password'=>$password)
至
array('email'=>$email, 'password'=>$password)
在我的一个配置文件的深处,有人(可能是我)在 PHP 文件的 <?
开头之前插入了 space。这是在设置任何 headers 之前,因此不允许更改它们(意味着不能设置 cookie),因为数据已经写入输出流。因此,如果您正在寻找发生这种情况的原因,请确保您正在寻找。
由于我遇到了类似的问题,所以还有一个问题可能是:
您忘记使用“web”中间件组。
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
// ... and more
],
如果不这样做,则永远不会设置排队的 cookie。