VinothPHP
VinothPHP

Reputation: 821

Use cakephp session with two different domains

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.

Upvotes: 2

Views: 2195

Answers (4)

KevinCoder
KevinCoder

Reputation: 221

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');?>

Upvotes: 3

Sliq
Sliq

Reputation: 16494

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.

Upvotes: 6

Jhanvi
Jhanvi

Reputation: 592

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.

Upvotes: 1

iamart
iamart

Reputation: 311

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.

Upvotes: 1

Related Questions