# Accessing session data from another application

**URL:** <https://www.ruby-forum.com/t/accessing-session-data-from-another-application/142423>\
**Category:** Rails\
**Created:** [July 7, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/accessing-session-data-from-another-application/142423 "2008-07-07T21:26:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 7, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/accessing-session-data-from-another-application/142423/1 "2008-07-07T21:26:44Z")

</div>

Hi,

I have my main Rails site running and the user\_id is stored inside the  
session. How can I access that info from within another application?

How do I decypher the session?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 7, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/accessing-session-data-from-another-application/142423/2 "2008-07-07T22:54:59Z")

</div>

On Jul 7, 8:26 pm, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,
> 
> I have my main Rails site running and the user\_id is stored inside the  
> session. How can I access that info from within another application?
> 
> How do I decypher the session?

What session store are you using?

Fred

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 8, 2008, 12:21am UTC](https://www.ruby-forum.com/t/accessing-session-data-from-another-application/142423/3 "2008-07-08T00:21:14Z")

</div>

Frederick C. wrote:

> On Jul 7, 8:26ï¿½pm, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Hi,
> > 
> > I have my main Rails site running and the user\_id is stored inside the  
> > session. How can I access that info from within another application?
> > 
> > How do I decypher the session?
> 
> What session store are you using?
> 
> Fred

If you use :cookie\_session\_store (the default in Rails2), and use the  
same application secret between the two applications,  
and you set a domain specific cookie, both should be able to access the  
same cookie.

config.action\_controller.session = {  
:session\_key =\> ‘\_my\_app\_session’,  
:secret =\>  
‘8ab332268appf980149adb591f20bfbb219a2029c5d7ee3c55c7a7439f6aed24d8a787591522adb1b2e16bd83b648642cf0ccfbfb10528bd4f47b9551012c1d’  
}  
config.action\_controller.session\_store = :cookie\_session\_store  
config.action\_controller.session\_options[:session\_domain] =  
“[mydomain.com](http://mydomain.com)”

if you have that the same in both,  
and your two applications run on [app1.mydomain.com](http://app1.mydomain.com) and [app2.mydomain.com](http://app2.mydomain.com)

that should be sufficient…

let me know if I’m wrong.

Mj

[http://workingwithrails.com/person/12394-matthew-rudy-jacobs](http://workingwithrails.com/person/12394-matthew-rudy-jacobs)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 8, 2008, 10:53am UTC](https://www.ruby-forum.com/t/accessing-session-data-from-another-application/142423/4 "2008-07-08T10:53:56Z")

</div>

On 8 Jul 2008, at 09:40, Fernando P. wrote:

> Hi thanks for your answers.
> 
> I am using cookie based session store. My second app is not a Rails  
> app,  
> it a php script. So from that php script how can I use the secret key  
> “8ab…” to decrypt the info inside the session?

It’s not a decryption key (ie the session is not encrypted). Consider  
a typical session:

BAh7BzoPdGV4cGVydF9pZGkE1pM2MiIKZmxhc2hJQzonQWN0aW9uQ29udHJv%0AbGx  
lcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D  
%3D–3729a7892e049aafce7e6e4db4210  
84f5886e4a9

The first bit (before the --) is the session itself. The second part  
is a cryptographically strong checksum of that data (using the secret  
set in environment.rb)  
The session data is just a Marshal.dump of the session, base64  
encoded. In ruby you can load it with

Marshal  
.load  
(CGI  
.unescape  
( “BAh7BzoPdGV4cGVydF9pZGkE1pM2MiIKZmxhc2hJQzonQWN0aW9uQ29udHJv  
%0AbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D  
%3D”).unpack(‘m\*’).first)

The hard bit for your php app will be implementing Marshal.load (if  
you’re lucky, someone has already done this).

Fred

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 8, 2008, 10:55am UTC](https://www.ruby-forum.com/t/accessing-session-data-from-another-application/142423/5 "2008-07-08T10:55:47Z")

</div>

Fernando P. wrote:

> Hi thanks for your answers.
> 
> I am using cookie based session store. My second app is not a Rails app,  
> it a php script. So from that php script how can I use the secret key  
> “8ab…” to decrypt the info inside the session?
> 
> Best regards,

hmm…  
maybe ignore that then.

so what is your setup,  
what is your requirement?

If you can access the same database,  
then you could do something like

adding a column  
add\_column :users, :session\_key, :string

then when someone logs in

current\_session\_key = @current\_user.set\_session\_key # create a random,  
unique string, and save it

cookie[:my\_session\_key] = {:domain =\> “[mydomain.com](http://mydomain.com)”, :value =\>  
current\_session\_key}

then in the php app

you’ll get sent the “my\_session\_key” cookie  
and do the equivalent of  
User.find\_by\_session\_key(cookie[:my\_session\_key])

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 8, 2008, 1:19pm UTC](https://www.ruby-forum.com/t/accessing-session-data-from-another-application/142423/6 "2008-07-08T13:19:01Z")

</div>

My users are currently logging in using restful\_authentication inside  
the rails app, so what I want is to retrieve the user\_id from within my  
php app so that I can identify who is the user who is sending me the  
request.

The best solution would be to find a way to unmarshal the cookie data  
from within php, but if that is too complicated to do (although the data  
is only a string) then I will use Mat’s solution.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 8, 2008, 10:40am UTC](https://www.ruby-forum.com/t/accessing-session-data-from-another-application/142423/7 "2008-07-08T10:40:29Z")

</div>

Hi thanks for your answers.

I am using cookie based session store. My second app is not a Rails app,  
it a php script. So from that php script how can I use the secret key  
“8ab…” to decrypt the info inside the session?

Best regards,
