# Setting session id for first visit

**URL:** <https://www.ruby-forum.com/t/setting-session-id-for-first-visit/151536>\
**Category:** Rails\
**Created:** [October 22, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/setting-session-id-for-first-visit/151536 "2008-10-22T16:41:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwgeller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/db5fbb/32.png) [@gwgeller](https://www.ruby-forum.com/u/gwgeller)\
**Post date:** [October 22, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/setting-session-id-for-first-visit/151536/1 "2008-10-22T16:41:17Z")

</div>

I am logging web visits with a before\_filter on the application  
controller. The issue I’m having is that the initial visit does not  
return a session id. I’m using the SqlSessionStore and the function  
MysqlSession.find\_session(session.session\_id) to retrieve the session  
id. If it doesn’t exist I use a -1. So looking in my visits table the  
initial visit always has -1 for the session id and the rest of the  
visits contain a valid session id for that visitor.

I tried putting the logging of visits in the after filter but that  
didn’t seem to work. Is this something that I can correct or is that  
just the way it is with sessions?

Thanks,  
Gunner

---

<div class="post-metadata">

**Author:** ![gwgeller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/db5fbb/32.png) [@gwgeller](https://www.ruby-forum.com/u/gwgeller)\
**Post date:** [October 22, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/setting-session-id-for-first-visit/151536/2 "2008-10-22T17:25:46Z")

</div>

It makes sense that the initial visit would not have a session id  
because depending on where you are assigning it, you might be logging  
the variable before setting it.

Also, you don’t really need to hang on to the session details when  
logging web visits. Unless you have a special reason to, i wouldn’t  
track any of the session details, they can easily be crated and  
compromised by scripts anyway. Try to keep session data to a minimum.

You might need to show some code if this doesn’t help.

Kent

gwgeller wrote:

> I am logging web visits with a before\_filter on the application  
> controller. The issue I’m having is that the initial visit does not  
> return a session id. I’m using the SqlSessionStore and the function  
> MysqlSession.find\_session(session.session\_id) to retrieve the session  
> id. If it doesn’t exist I use a -1. So looking in my visits table the  
> initial visit always has -1 for the session id and the rest of the  
> visits contain a valid session id for that visitor.
> 
> I tried putting the logging of visits in the after filter but that  
> didn’t seem to work. Is this something that I can correct or is that  
> just the way it is with sessions?
> 
> Thanks,  
> Gunner

---

<div class="post-metadata">

**Author:** ![gwgeller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/db5fbb/32.png) [@gwgeller](https://www.ruby-forum.com/u/gwgeller)\
**Post date:** [October 22, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/setting-session-id-for-first-visit/151536/3 "2008-10-22T17:52:27Z")

</div>

Kent,  
I guess I really haven’t thought it through, but my original  
purpose was to group a visit using the session id as opposed to an ip  
which could potentially be the same for two visitors. You mentioned  
where I set the session id, but I don’t do it, SqlSessionStore does it  
for me, but I’m curious has to how that works. Do you know when the  
session would be set?

On Oct 22, 10:25 am, Kent F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gwgeller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/db5fbb/32.png) [@gwgeller](https://www.ruby-forum.com/u/gwgeller)\
**Post date:** [October 22, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/setting-session-id-for-first-visit/151536/4 "2008-10-22T18:40:39Z")

</div>

At work we do something similar, although we dont have issues with the  
session\_id using ActiveRecordStore. One approach you could take would  
be to create a new object (Visitor), and store the visitor.id in the  
session. Then use the before filter to load that Visitor on each  
request. Then, create an Event associated with that visitor for each  
request (this allows you to have multiple event\_types). Then when  
doing your queries you can do visitor.events to see what each person  
did. This approach allows us to clean the session table out every few  
days so it doesnt get to slow. The visitor table only gets written to  
once for each person and then is read only going forward.
