tags:

views:

48

answers:

1

I am using the database session driver in Kohana v2. To make sessions persistent, Kohana creates a token cookie. This cookie uses the cookie configuration I suppose.

When I set a session like this:

$this->session->set('UserID', $user->UserID);

The session variable UserID is availble even when the browser is closed. Nice.

The cookie uses this config setting:

$config['domain'] = '.mydomain.com';

How can I set the domain when I set the session variable? Every user has it own sub-domain, so it is a dynamic value.

+2  A: 

You could figure out what subdomain you're on right now in index.php before the bootstrapping process, and then include that variable in the cookie config file. Something like (untested):

$myDomain = 'mydomain.com' ;
$currDomain = $_SERVER['SERVER_NAME'];
$subDomain = '' ;

//remove www if needed
if (substr($currDomain, 0, 4) == 'www.') {
    $currDomain = substr($currDomain, 4) ;
}

$currDomainPos = strpos($currDomain, $myDomain) ;
if ($currDomainPos !== false) { //sanity check, myDomain string must exist
    if ($currDomainPos !== 0) {
        //got subdomain since SERVER_NAME doesn't start with myDomain
        $subDomain = substr($currDomain, 0, $currDomainPos) ;
    }
}

Then in cookie config:

$config['domain'] = "$subDomain.mydomain.com" ;
Fanis
Thanks! I put the code right in the cookies config file. Never realised that you can code in the config files. Very obvious know I now it..
koen