Session management in 1 database on shared hosting

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
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Session management in 1 database on shared hosting

Post by GeertDD »

A quick question that just crossed my mind. Having a shared hosting package, I've been storing my sessions in a database for some time now. Works great. The thing is that in every domain pointer's database I created a new table called "_sessions".

Now I thought: why not create one central database that stores all sessions for every domain on my account?

Advantages? No need to create that same "_sessions"-table over and again. More generic setup. Keeps databases 'cleaner'.

Disadvantages?

(As far as I know, by default all the sessions on shared hosting are stored in the tmp/ directory so I guess storing all my sessions in one database should be no problem. Or I am overlooking something here?)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're not overlooking much of anything. The session id will generally be unique across the server.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Great. However, I just did think of some disadvantages.

I have a field called "userid" in my sessions table. That way I can easily extract data for stuff like "who is online" pages. When using a different database for storing sessions, joining the sessions table with the users table is not possible anymore.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why wouldn't be possible? If you're worried about having them selectable with respect to each domain then you could simply store a domain reference (of some fashion) in the table at the time of creation.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

You're right about that domain reference. I did not even think about that yet myself.

I was wondering whether it would be possible to link two tables that are in a different database.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's certainly possible. Although you may need to build an artificial link via logic in your PHP code.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

You mean like first selecting the online user IDs from the sessions table. Then selecting the usernames and other info from the users with those ID's. However, I don't feel like going through the hassle of splitting this up, and adding domain references, etc.

This topic was just a quick thought of mine. Now I know the intended setup should be possible, but in the end I prefer to leave things as they are. It still works after all. Thank you for the input, feyd.
Post Reply