Search code examples
wordpresssessionmultisite

How to set and get common variable in session for MultiSite WordPress?


I am using MultiSite WordPress. I want to set and get common variable in session/cookies for all sites.

My sites are:

NetWrok site:

http://www.sitename.com

SubDomain sites are:

http://hindi.sitename.com

and

http://urdu.sitename.com

The problem is when I set session value in "http://www.sitename.com" but not able to get that value in "http://hindi.sitename.com" and "http://urdu.sitename.com"

Is there any other solution to do this?

Thanks in advance


Solution

  • Using session cookies across subdomains is a well known problem: PHP Sessions across sub domains

    However if you want to share common informations across your wordpress multisite sites the following may work (as :

    $some_name = session_name("shared_multisite_sesssion");
    session_set_cookie_params(0, '/', '.sitename.com');
    session_start();
    

    Use only cookies for sessions

    session.use_only_cookies = 1
    session.cookie_httponly = 1
    

    Set the following parameteres in your WordPress config:

    define( 'COOKIE_DOMAIN', '.sitename.com' ); // Dot prefix
    define( 'COOKIEPATH',    '/' );
    define( 'COOKIEHASH',    md5( 'sitename.com' ) );
    

    However there are a few warnings you should take into account:

    • WordPress multisite sites are sometimes independent - as soon as they have their own domain this will not work anymore (Share a cookie between two websites)
    • WordPress multisite sites in many setups are intended to be individual sites with similar design. It may not be the best architectural idea to connect those sites via common sessions.