Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skovmand's avatar

Set cookies on root domain

Hi!

I have a website which allows both access to www.mywebsite.com and mywebsite.com.

However this gives me a cookie problem, because a user which logs on to www.mywebsite.com does not have a session on mywebsite.com, because the cookie domain is set to the domain in use.

Is there a way in Laravel to force cookies to be set on the root mywebsite.com? This would remove the problem. I tried changing the session domain in Session.php without luck.

Thanks!

0 likes
3 replies
phildawson's avatar
Level 26
    /*
    |--------------------------------------------------------------------------
    | Session Cookie Domain
    |--------------------------------------------------------------------------
    |
    | Here you may change the domain of the cookie used to identify a session
    | in your application. This will determine which domains the cookie is
    | available to in your application. A sensible default has been set.
    |
    */
    'domain' => '.mywebsite.com',
2 likes
skovmand's avatar

Thanks! I'll try it out.

Also, found it myself in CookieServiceProvider.php. It is indeed set from Session.php:

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('cookie', function ($app) {
            $config = $app['config']['session'];

            return (new CookieJar)->setDefaultPathAndDomain($config['path'], $config['domain']);
        });
    }

Please or to participate in this conversation.