The problem is maintaining a session between two different domains on a website run in CakePHP

Well, as I wrote earlier ... I created a website in two languages. One with the URL www.mainDomain.com (English) and the other with www.fr.subDomain.com (French).

Both are made in CakePHP, in French I just changed my views on French. But the problem is that when someone goes to the English version and then switches to the French version, the session does not recognize it and again asks for a login. This was the biggest mistake in the web application I have made so far.

For this, since Swanney told me to go through the link , and I did it in my application, as the link said, he worked to log in to the system, which shared the session between the two domains (the main domain and its subdomain). But when I checked it completely, I realized that both sites drop the latest NEWS from the database, both of which are different. Just to check if I was wrong, I changed some save variable to the database in the session array. But now he did not remember anything (session). Can someone suggest me what could be the problem with this and how can I resolve this ... ???

Thanks in advance

+6
php session cakephp
source share
3 answers

I'm not sure I fully understand, but I will try. I think this is about setting up PHP called session.cookie_domain .

Assuming your websites have the following URLs:

You want: .example.org .

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

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

If your websites run on two completely different domains, for example:

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

+3
source share

@dooltaz This is a great solution. Be sure the cake seems to be setting the cookies for me. Instead, I sent the redirection method to user ro, and then moved 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 a typo in your code is fixed ..)

+3
source share

If you have two different domains, I would suggest the following:

On the page "www.mainDomain.com" put a link to the site "www.fr.subDomain.com" and transfer the cookie in a file of the form:

 $session_cookie = $_COOKIE[Configure::read('Session.cookie')]; echo $html->link('See French Site', 'http://www.fr.subDomain.com/?session_key='.$session_cookie); 

Then on the French site, add some code to simulate cookies in app_controller.php> beforeFilter ().

 function beforeFilter() { if(!empty($this->params['url']['session_key']) { // Setup variables here... setcookie(Configure::read('Session.cookie'), $session_cookie, time()+360000, '/', $domain); // You could use CAKE setcookie command here. } } 

Now that the cookies are the same, you will have to either use database sessions or cake based sessions. Read the instructions in core.php to install them.

This will allow you to basically use the same session on different sites. I am actually in the middle of implementing an ACL on multiple sites with the same login. It may seem a little complicated, but just do it step by step, everything will be fine. Also, don't be afraid to jump into the core Cake code to see how it works.

+2
source share

All Articles