2

I have two different domains named http://sample.com and http://example.com. Sample website developed by CAKEPHP and Example website developed by corephp. If I am going to login to example.com, this session should match with cakephp session. If I have logged anyone of website, It will act for both. Is this possible. Please suggest me what is the best way to workout it.

Thanks in Advance.

VinothPHP
  • 821
  • 2
  • 10
  • 22
  • 1
    Have you looked at SSO http://stackoverflow.com/questions/15188261/single-sign-on-sso-between-wordpress-and-cakephp – RobertPitt Aug 16 '13 at 08:29
  • I think you have to maintain session in database manually, you can set status field to check user logged in or not. – Er.KT Aug 19 '13 at 04:49

4 Answers4

6

There's a common technique to run different sites/domains on a single server, called vhosts. As you can specify the session path for every vhosts, it's also possible to define the same path for all vhosts, which will result in shared sessions. A session created in project A is then also available in session B and other way round.

The vhosts don't care if you use pure PHP, CakePHP or whatever. Session is Session.

The vhost rules you need are (note that the session folder is the same in both definitions):

<VirtualHost *:80>
    ServerName www.sample.com
    DocumentRoot /var/www/xxx1
    <Directory "/var/www/xxx1">
        AllowOverride All
        php_value session.save_path "/var/mysessionfolder"
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /var/www/xxx2
    <Directory "/var/www/xxx2">
        AllowOverride All
        php_value session.save_path "/var/mysessionfolder"
    </Directory>
</VirtualHost>

To get more info on that it might be useful to read this thread: How to prevent PHP sessions being shared between different apache vhosts? which ask the opposite of what you want.

Community
  • 1
  • 1
Sliq
  • 15,937
  • 27
  • 110
  • 143
3

Well cake stores its session data in a custom session name , so if you put the following before the session_start(); on the none cakePHP site you should be able to access all the session data:

<?php session_name('CAKEPHP');?>
KevinCoder
  • 191
  • 5
1

You need to connect to the first website's database and compare user session with user cookies.

$db = new mysqli("localhost", "user", "pass", "db");

$id         = $db->escape_string($_COOKIE["id"]);
$session    = $db->escape_string($_COOKIE["session"]);

$check      = $db->query("SELECT data FROM cake_sessions WHERE id = '" . $db->real_escape_string($id) . "'");
$checkarr   = $check->fetch_assoc();

if($session == $checkarr["data"])
    echo "logged";

not tested; also I'm not sure about cakephp authcookes, so they're probably wrong.

iamart
  • 311
  • 3
  • 15
  • You are right Desire, But I have tried already using cake session from DB, I got the same values, But My requirement is, If I'm logged in anyone of the site, It will act for both, and If I passed those session datas to cakephp, It doesn't match with cakephp sessions. This is my problem, Its again ask for login... Thanks for your reply... – VinothPHP Aug 16 '13 at 08:42
  • @VinothPHP So, if I understand you correctly, you want to log in user on one website automatically if he has already been logged in on another, right? – iamart Aug 16 '13 at 08:50
  • yes, Thats right... If I logged into corephp website, it will automatically logged into cakephp and sessions should match with cakephp, thats my problem, Check my question pls... – VinothPHP Aug 16 '13 at 09:06
  • @VinothPHP your question does not ask how to login to two domains at once - it asks how to share sessions between them. Please amend your question to make that clear, otherwise readers will focus on the title. – AD7six Aug 16 '13 at 13:33
  • @AD7six Yeah, This question based on match the sessions, I have logged into Corephp project, Its logged in, and I'm unable to use the session to cakephp framework, I have to match the session between both projects, Because,If I login to anyone of the website, Its automatically logged into another one. this is what my requirement. – VinothPHP Aug 16 '13 at 14:26
  • @VinothPHP okay, how do you want to do that? I mean, do your users have to be registered on both sites; or they have to be registered only on one website? – iamart Aug 16 '13 at 23:47
1

You Can Use third party authentication service like CAS vendor and component in combination of Auth component and can maintain global session between two domains.

Jhanvi
  • 594
  • 3
  • 17