5中跨子域的持久会话

5中跨子域的持久会话

本文介绍了在Laravel 5中跨子域的持久会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用5.0

我已经设置了'domain' => '.example.com',但是它不起作用.我无法在这样的一个域上持续进行会话.

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.

我的网站有很多子域:

vancouver.example.com
newyork.example.com

等等...它们托管在同一服务器上,并且是同一Laravel应用(共享相同的存储目录)

etc... they are hosted on the same server and are the same Laravel app (share the same storage directory)

我使用正确的凭据登录,应用程序根据该凭据重定向到网站上的另一个页面,并且那时我没有会话.即使我使用正确的凭据登录,var_dump(Auth::user())仍显示null.

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.

storage/framework/sessions在那里显示了14个不同的文件,它们全都适合我,我在开始测试之前就将它们清除了.

storage/framework/sessions shows 14 different files there, they are all for me and I cleared them out before I started testing this.

我将在下面附加我的AuthController @ postLogin方法,如果session.php 'domain' => null

I'll attach my AuthController@postLogin method below, which works fine if 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(),
                ]);
}

推荐答案

弄清楚了.在session.php中更新domain => '.example.com'并清除所涉及站点的cookie.

Figured it out. Update domain => '.example.com' in session.php and clear the cookies for the site in question.

这篇关于在Laravel 5中跨子域的持久会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:45