Updating sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Daz
Forum Newbie
Posts: 18
Joined: Thu Mar 19, 2009 2:12 am

Updating sessions

Post by Daz »

I have a db table to store sessions for the sole purpose of admins being able to see active visitors on the site. One of the cells is a timestamp field.

Code: Select all

IF ( UPDATE_SESSIONS )     {
     Remove sessions older than ten minutes
} ELSE {
    There is no current session - INSERT one
}
Question #1
If the page is refreshed too fast (so that both page calls have the same timestamp) the 2nd page errors for trying to INSERT a duplicate. Is there anyway to avoid this other than putting a try/catch around the INSERT statement?

Question #2
Are there any real benefits to change MySQL's type to MEMORY for the sessions table?
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Updating sessions

Post by iankent »

Question #1 - The best way to avoid it would be to use WHERE NOT EXISTS, e.g.

Code: Select all

INSERT INTO table (a,b,c) VALUES (x,y,z) WHERE NOT EXISTS (SELECT a FROM table WHERE a=x AND b=y AND c=z);
That way it'll only insert the values if the SELECT query returns no results

Question #2 - Not a clue sorry :)
edit: actually... if you use MEMORY it'll be faster running SELECTS but your server will require more RAM than it currently does (with lots of users, that could mean a lot of extra RAM). It also means if your mysql server is restarted for any reason, or if your server crashes/loses power, all contents of the table are gone. Having said that, performance improvements on a SELECT from memory table probably isn't going to be all that noticeable, so personally I wouldn't bother, but it'll depend on your requirements.

hth
Daz
Forum Newbie
Posts: 18
Joined: Thu Mar 19, 2009 2:12 am

Re: Updating sessions

Post by Daz »

Exactly what I was looking for -thanks!
Post Reply