Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem in maintaining session between two different domains on a website done in CakePHP

Well as I have posted earlier too...I have created a site in two languages. One with URL www.mainDomain.com (English) and other with www.fr.subDomain.com (French).

Both are done in CakePHP,in french I have just changed the views of it to French. But the problem is, when anybody login's in English version and then switches to the French version, the session doesn't recognizes it and ask for login again. It has become to be the biggest bug in the Web application which I have done till far.

For that, as Swanny told me to go through a link and I did it on my application as it was said on the link.Apparently,it worked for login which shared session between two domains(main domain and it's subdomain). But when I checked it thoroughly, I recognized that both the sites are throwing the latest NEWS from Database, both data are different. Just to check if I was wrong I changed the some save variable to database in session array. But now it refused to remember anything (session). Could anyone suggest me what could be problem with this and how can I resolve this...???

Thanks in advance

like image 726
Nishant Shrivastava Avatar asked Aug 18 '09 09:08

Nishant Shrivastava


2 Answers

I'm not sure I completely understand, but I'm gonna try. I think this is about a PHP setting called session.cookie_domain.

Assuming your websites have the following URLs:

  • http://www.example.org/
  • http://fr.example.org/
  • http://de.example.org/

The setting you want is: .example.org.

You can adjust this in php.ini, a .htaccess file or even in PHP itself:

<?php ini_set('session.cookie_domain', '.example.org'); ?>

If your websites run on two completely different domains, e.g.:

  • http://example1.org/
  • http://example2.org/

... then there is no way to share the cookie between these two different domains.

like image 78
Till Avatar answered Oct 05 '22 01:10

Till


@dooltaz That is a great solution. Be issue is that cake seems to be setting the cookie after me. What I did instead is send the user ro a redirect method and then move the cookie setting to the afterFilter

    function afterFilter() {
    if (!empty($this->params['url']['session_key'])) {
        // Setup variables here...
        setcookie(Configure::read('Session.cookie'), $this->params['url']['session_key'], time()+360000, '/');
        // Cakes cookie method will put your cookie name in [] so it does not work.
    }
}

(Also fixed typo in your code..)

like image 23
Ryan White Avatar answered Oct 05 '22 03:10

Ryan White