Sharing Session over subdomains in WordPress

Sometime we need to share login session into our subdomains. The user/member will not have to login again in the subdomains.

PHP in general,  we usually use
ini_set('session.cookie_domain', '.domain.tld');
if ( empty(session_id()) ) session_start();

Prefixed with ‘.’ in front of domain, on session.cookie_domain setting.

If familiar with CodeIgniter, just set in the config as
$config['cookie_domain'] = ".domain.tld";
How about in WordPress? It’s also simple, set in your wp-config.php
define('COOKIE_DOMAIN', '.domain.tld');
This session only can be shared into a single WordPress installation. Which mean, if you have other subdomains with another WordPress installation, this configuration might not work, since WordPress has another session for other installation. You have to work around to send your login session into another.

I am not testing yet to share session in the same domain but has different installation. For eg: main site is domain.tld has installation itself and the other, blog.domain.tld also has WordPress installation itself. But will try in another time. If you have experienced with this, please share.

Categorised in:

This post was written by Sunaryo Hadi

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.