# Modify Session Data using session\_id

**URL:** <https://www.ruby-forum.com/t/modify-session-data-using-session-id/155664>\
**Category:** Rails\
**Created:** [December 15, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/modify-session-data-using-session-id/155664 "2008-12-15T12:41:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![renian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/87869e/32.png) [@renian](https://www.ruby-forum.com/u/renian)\
**Post date:** [December 15, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/modify-session-data-using-session-id/155664/1 "2008-12-15T12:41:12Z")

</div>

Hi, first post here. I’m enjoying getting to know rails. I have been  
scratching my head trying to find a way to modify data in a session by  
directly accessing CGI::Session.

I should note, using the following code, I can successfully create a new  
key / value pair from the console, but not within my app. I have tried  
both methods below:

def add\_session\_data(other\_sess\_id, new\_data)  
a =  
CGI::Session::ActiveRecordStore::Session.find\_by\_session\_id(other\_sess\_id)  
a.data[:test] = new\_data  
a.save  
end

def add\_session\_data(other\_sess\_id, new\_data)  
cgi = CGI.new(“html4”)  
a = CGI::Session.new(cgi, ‘database\_manager’ =\>  
CGI::Session::ActiveRecordStore, ‘session\_id’ =\> other\_sess\_id)  
a[:test] = new\_data  
a.close  
end

---

<div class="post-metadata">

**Author:** ![renian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/87869e/32.png) [@renian](https://www.ruby-forum.com/u/renian)\
**Post date:** [December 15, 2008, 1:24pm UTC](https://www.ruby-forum.com/t/modify-session-data-using-session-id/155664/2 "2008-12-15T13:24:48Z")

</div>

Hi,  
Rather than accessing a base methods directly, you can access some  
class methods like ‘process\_cgi’ to solve you pbm better I think. Try

> **[ActionController::Base](https://api.rubyonrails.org/classes/ActionController/Base.html#M000523)**
>
> Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed on request and then either it renders a template or redirects to another action.

this one and post here whether it helps you or not. I am also  
interested in knowing this.

Thanks,  
Sadeesh

On Dec 15, 4:41 pm, Jeff V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![renian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/87869e/32.png) [@renian](https://www.ruby-forum.com/u/renian)\
**Post date:** [December 16, 2008, 1:15am UTC](https://www.ruby-forum.com/t/modify-session-data-using-session-id/155664/3 "2008-12-16T01:15:08Z")

</div>

Sadeesh Viswanthan wrote:

> Hi,  
> Rather than accessing a base methods directly, you can access some  
> class methods like ‘process\_cgi’ to solve you pbm better I think. Try  
> [ActionController::Base](http://api.rubyonrails.org/classes/ActionController/Base.html#M000523)  
> this one and post here whether it helps you or not. I am also  
> interested in knowing this.
> 
> Thanks,  
> Sadeesh
> 
> On Dec 15, 4:41ï¿½pm, Jeff V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks for the reply.

I played around with process\_cgi and determined it wouldn’t work for  
this application. Further, I went into the code for action\_controller  
to see how session data is saved. I went as far as writing my own  
method that repeats the exact way session data is saved in Action  
Controller, but no dice.

What I think is happening, is when a request is made by the client, it  
grabs the current session data for a given session\_id, THEN executes  
application code (including updating in memory any changes to the  
session data, THEN updates the database.

When we force changes to the session (by selecting sessions.data by  
session\_id, modifying, and saving) INSIDE of a request routine, the data  
changes just fine, but is overwritten at the end of the request.

I think.

If anybody has an idea to access whatever is the temporary store for the  
session when it gets changed during a request, I’d love to figure this  
out.

---

<div class="post-metadata">

**Author:** ![renian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/87869e/32.png) [@renian](https://www.ruby-forum.com/u/renian)\
**Post date:** [December 16, 2008, 2:08am UTC](https://www.ruby-forum.com/t/modify-session-data-using-session-id/155664/4 "2008-12-16T02:08:14Z")

</div>

I just realized that since the objective is to modify another user’s  
session data (as opposed to my own), the code I am using will work.  
There’s one caveat, however, and someone correct me if I’m wrong: If  
the user who’s session data we are changing makes a request at the exact  
same time as our user who is modifying data, there’s a chance that he  
will overwrite our changes, and there is no easy way to detect this. Or  
is this not true in a single-threaded environment?

That being said, I’m also slightly embarrassed to post the following  
code, I have a feeling there’s a much better way to do this. But here  
goes:

@@connection ||= ActiveRecord::Base.connection  
record = @@connection.select\_one(“SELECT \* FROM sessions WHERE  
session\_id = ‘#{other\_sess\_id}’”)  
a = record[‘data’]  
b = Marshal.load(ActiveSupport::Base64.decode64(a)) #demarshal and load  
session into b  
b[:testdata] = ‘data here’  
c = ActiveSupport::Base64.encode64(Marshal.dump(b)) #marshal  
@@connection.update(“UPDATE sessions SET data = ‘#{c}’ WHERE session\_id  
= ‘#{other\_sess\_id}’”) #save to db
