Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cookies - use session id? 1

Status
Not open for further replies.

philote

MIS
Oct 2, 2003
861
US
I'm creating a shopping cart web app using perl and mysql. I want it to be like most other carts on the web where users can click on items to add them to their cart. I want these items to be in their cart only for the lenght of their current session. What's the best way to do this. I know I can just store the items in their cart as cookies but I also read that I could instead just store a unique session id for them as a cookie and store their items in the database. I think I could do the cookie method but how would I go about implementing session id's? Specifically, how would I create them uniquely and how would I know when their sesssion's over so I could delete their items from the database?

Kevin
A+, Network+, MCP
 
Check out CGI::Session, it will allow you to manage this sort of thing very easily.


As for session expiration, you can reset the cookie on every request ( yuck! ) and that would reset the length of time their session will be alive.

The far better method is to check a time variable in their session on each request and if its to old log them out. if its acceptable, update that time and continue.
 
Awesome! That seems to be exactly what I was looking for.

What I was most concerned about was my database of session information growing to a large size because the user's session would expire and my script wouldn't know to delete the session information. But it looks like CGI::Session can somehow expire the session after a set period of time and 'hopefully' clear the database records for that session.

Kevin
A+, Network+, MCP
 
I don't think CGI:Session auto-clears the DB ( I may be wrong, I've never used it specifically ) but if it doesn't do what I usually do. In your sessions table add a 'TIMESTAMP' column. This will update on every session update. You can then delete the data after a defined period of inactivity.

Good luck!
 
Does CGI::Session automatically set the client's session cookie or do I have to do this in cgi.pm's header function? And if I don't have to set it, I'd assume I'd at least have to create the session before calling header(), right?

siberian, do you have a short example on how to use CGI::Session? Maybe that'll answer my current question and any future questions I'm sure I'll have.

Thanks!

Kevin
A+, Network+, MCP
 
man CGI::Session

That should answer all your questions.

I never have used it, I use Apache::Session under mod_perl.
 
Thanks anyway. I had already looked at all the docs I could find, none of which answered my question. I'll do some more searching and experimenting. I AM running Apache and mod_perl, so maybe I'll check out Apache::Session as well.

Kevin
A+, Network+, MCP
 
Oh yes, if you are running modperl Apache::Session is the only way to go!

Apache session does cookie management etc for you. Its great.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top