Connecting Tech Pros Worldwide Forums | Help | Site Map

keeping session data across two domains

laredotornado@zipmail.com
Guest
 
Posts: n/a
#1: Sep 24 '07
Hi,

I'm using PHP 4.4.4. I have two domains -- www.mydomain1.com and
www.mydomain2.com. Both point to the same IP address. I have two
pages on that IP -- first.php

<?php
session_start();
$_SESSION['test'] = "hello";
?>

and second.php

<?php
session_start();
print $_SESSION['test'];
?>

What I would like is when I first visit http://www.mydomain1.com/first.php
and then visit http://www.mydomain2.com/second.php to have the word
"hello" printed. Does anyone know how to adjust the above scripts or
my environment to make this possible?

Thanks, - Dave


C.
Guest
 
Posts: n/a
#2: Sep 24 '07

re: keeping session data across two domains


On 24 Sep, 20:58, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
Quote:
Hi,
>
I'm using PHP 4.4.4. I have two domains --www.mydomain1.comandwww.mydomain2.com. Both point to the same IP address. I have two
pages on that IP -- first.php
>
<?php
session_start();
$_SESSION['test'] = "hello";
?>
>
and second.php
>
<?php
session_start();
print $_SESSION['test'];
?>
>
What I would like is when I first visithttp://www.mydomain1.com/first.php
and then visithttp://www.mydomain2.com/second.phpto have the word
"hello" printed. Does anyone know how to adjust the above scripts or
my environment to make this possible?
>
Thanks, - Dave
I'll assume you're using cookies for sessions. In which case the
question is how you get a cookie from one site set when you are
accessing another.

The solution is to suck in pages from both mydomain1 and mydomain2 at
the point where the session is established. This could be done with
frames or by redirection. Life's probably a lot simpler if you pass
across the generated session id from one to the other, but you need to
be wary of session fixation. Otherwise you'll probably need to write
your own session handler to maintain 2 sessions alive and in sync.

HTH

C.

laredotornado@zipmail.com
Guest
 
Posts: n/a
#3: Sep 24 '07

re: keeping session data across two domains


On Sep 24, 3:51 pm, "C." <colin.mckin...@gmail.comwrote:
Quote:
On 24 Sep, 20:58, "laredotorn...@zipmail.com"
>
>
>
>
>
<laredotorn...@zipmail.comwrote:
Quote:
Hi,
>
Quote:
I'm using PHP 4.4.4. I have two domains --www.mydomain1.comandwww.mydomain2.com. Both point to the same IP address. I have two
pages on that IP -- first.php
>
Quote:
<?php
session_start();
$_SESSION['test'] = "hello";
?>
>
Quote:
and second.php
>
Quote:
<?php
session_start();
print $_SESSION['test'];
?>
>
Quote:
What I would like is when I first visithttp://www.mydomain1.com/first.php
and then visithttp://www.mydomain2.com/second.phptohave the word
"hello" printed. Does anyone know how to adjust the above scripts or
my environment to make this possible?
>
Quote:
Thanks, - Dave
>
I'll assume you're using cookies for sessions. In which case the
question is how you get a cookie from one site set when you are
accessing another.
>
The solution is to suck in pages from both mydomain1 and mydomain2 at
the point where the session is established. This could be done with
frames or by redirection. Life's probably a lot simpler if you pass
across the generated session id from one to the other, but you need to
be wary of session fixation. Otherwise you'll probably need to write
your own session handler to maintain 2 sessions alive and in sync.
>
HTH
>
C.- Hide quoted text -
>
- Show quoted text -
Thanks for your response, C. Regarding
Quote:
Life's probably a lot simpler if you pass
across the generated session id from one to the other
hate to be dense, but how do you do that? - Dave

C.
Guest
 
Posts: n/a
#4: Sep 25 '07

re: keeping session data across two domains


On 24 Sep, 21:59, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
Quote:
On Sep 24, 3:51 pm, "C." <colin.mckin...@gmail.comwrote:
>
>
>
Quote:
On 24 Sep, 20:58, "laredotorn...@zipmail.com"
>
Quote:
<laredotorn...@zipmail.comwrote:
Quote:
Hi,
>
Quote:
Quote:
I'm using PHP 4.4.4. I have two domains --www.mydomain1.comandwww.mydomain2.com. Both point to the same IP address. I have two
pages on that IP -- first.php
>
Quote:
The solution is to suck in pages from both mydomain1 and mydomain2 at
the point where the session is established. This could be done with
frames or by redirection. Life's probably a lot simpler if you pass
across the generated session id from one to the other, but you need to
be wary of session fixation. Otherwise you'll probably need to write
your own session handler to maintain 2 sessions alive and in sync.
>
Quote:
HTH
>
Quote:
C.- Hide quoted text -
>
Quote:
- Show quoted text -
>
Thanks for your response, C. Regarding
>
Quote:
Life's probably a lot simpler if you pass
across the generated session id from one to the other
>
hate to be dense, but how do you do that? - Dave
When you start the session on, say domain1, include an iframe with a
hidden div, and pass the sessionid to a page in domain2 which sets a
session cookie:

e.g. www.domain1.com/logged_in.php...

<?php
if (session_id()=='') {
create_new_session=true;
}
session_start();

// .... start doing the page header and body...

// ... at the very end of the page, before the </bodytag....

if (create_new_session) {
session_commit();
$url="www.domain2.com/sync_session.php?usesess=";
$url.=base64encode(encrypt(session_id() . '/' . time(),
's3cr3t'));
// I've not spelled out how to use mcrypt
print "<iframe src=\"$url\" style=\"width:10px;height:5px\"></
iframe>\n";
// nor added the css to make it invisible
}
?>

.....and www.domain2.com/sync_session.php:

<?php

$request_session=decrypt(base64decode($_GET['usesess']), 's3cr3t');
list($use_id,$requested)=explode('/',$request_session);

if ($requested<time()+10) {
// allow a 10 second window to reduce probability of replay attacks
// although a more complete solution would be to set a session
variable in domain1 as a visa and
// reset it here.
set_cookie(session_name(), $use_id);
print "OK, using same session id";
} else {
print "Invalid sync request";
}

?>

....or something like that. Not tested - YMMV.

C.

Closed Thread