在 Laravel 5 中跨子域保持会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30338518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Persisting sessions across subdomains in Laravel 5
提问by Anthony Vipond
Using 5.0
使用 5.0
in config/session.php I have set 'domain' => '.example.com'
but it is not working. I cannot persist a session on even one domain like this.
在 config/session.php 中我已经设置'domain' => '.example.com'
但它不工作。我什至无法在这样的一个域上保持会话。
My site has many subdomains:
我的网站有许多子域:
vancouver.example.com
newyork.example.com
etc... they are hosted on the same server and are the same Laravel app (share the same storage directory)
等等...它们托管在同一台服务器上并且是同一个 Laravel 应用程序(共享同一个存储目录)
I login with the correct credentials, upon which the app redirects to another page on the site, and I have no session at that point. var_dump(Auth::user())
shows null
even though I logged in with the correct credentials.
我使用正确的凭据登录,然后应用程序重定向到站点上的另一个页面,此时我没有会话。即使我使用正确的凭据登录,也会var_dump(Auth::user())
显示null
。
storage/framework/sessions
shows 14 different files there, they are all for me and I cleared them out before I started testing this.
storage/framework/sessions
在那里显示了 14 个不同的文件,它们都是给我的,我在开始测试之前将它们清除了。
I'll attach my AuthController@postLogin method below, which works fine if session.php 'domain' => null
我将在下面附上我的 AuthController@postLogin 方法,如果 session.php 可以正常工作 'domain' => null
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if ($this->auth->attempt($credentials, $request->has('remember'))) {
Session::flash('message', 'You are now logged in.');
Session::flash('status', 'success');
if (str_contains($_SERVER['HTTP_REFERER'], '?goto=')) {
$params = explode('?', $_SERVER['HTTP_REFERER'])[1];
$target = explode('=', $params)[1];
} else {
$target = '/';
}
return redirect($target);
}
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
}
回答by Anthony Vipond
Figured it out. Update domain => '.example.com'
in session.php and clear the cookies for the site in question.
弄清楚了。domain => '.example.com'
在 session.php 中更新并清除相关站点的 cookie。
回答by engr.waqas
@gadss
@gadss
you need to add session table like this
你需要像这样添加会话表
php artisan session:table
composer dump-autoload
php artisan migrate
and change .env to
SESSION_DRIVER=database
并将 .env 更改为
SESSION_DRIVER=database
also modify config/session.php
还要修改 config/session.php
'driver' => env('SESSION_DRIVER', 'database')
and
'driver' => env('SESSION_DRIVER', 'database')
和
'domain' => '.yourdomain.com'
after that clear your browser's cache and cookies.
之后清除浏览器的缓存和 cookie。
回答by Kiran Maniya
You'll need to update the session configuration to persist the session in domain-wide including subdomains. Follow the steps given below.
您需要更新会话配置以在域范围内(包括子域)中保留会话。按照下面给出的步骤操作。
Go to
config/session.php
and update thedomain
with prefix.
asconfig => '.your-domain.com'
.Then clear your application cache, Open the Chrome DevTool and Go to Application > Application > Clear Storage. You'll need to clear out the previous cookiesalso.
run artisan command
php artisan config:cache
orphp artisan config:clear
to drop previously cached laravel application configs.
转到
config/session.php
并更新domain
前缀.
为config => '.your-domain.com'
。然后清除您的应用程序缓存,打开 Chrome DevTool 并转到Application > Application > Clear Storage。您还需要清除以前的cookie。
运行 artisan 命令
php artisan config:cache
或php artisan config:clear
删除以前缓存的 Laravel 应用程序配置。
If you are using databaseas the session driver, You need to create a session table for that. run command php artisan session:table
to generate the session table migration and then migrate it using php artisan migrate
. Then perform the three steps given above.
如果您使用数据库作为会话驱动程序,您需要为此创建一个会话表。运行命令php artisan session:table
生成会话表迁移,然后使用php artisan migrate
. 然后执行上面给出的三个步骤。
回答by BrokenBinary
Have you tried storing the sessions in the database, memcached, or redis instead of in files? I had a similar situation to yours and storing sessions in the database solved the issue for me.
您是否尝试将会话存储在数据库、memcached 或 redis 中而不是文件中?我遇到了与您类似的情况,在数据库中存储会话为我解决了这个问题。
For some reason Laravel's session driver doesn't handle cross domain sessions correctly when using the file driver.
出于某种原因,Laravel 的会话驱动程序在使用文件驱动程序时无法正确处理跨域会话。
回答by huuthang
If someone still gets the problem with subdomain cookie. Try to change Session Cookie Name in config/session.php
如果有人仍然遇到子域 cookie 的问题。尝试更改 config/session.php 中的会话 Cookie 名称