Page 1 of 1
Multiple clients on a server
Posted: Thu Feb 25, 2010 11:44 am
by barb woolums
Hi there,
I have a database driven website where I use sessions to store user names, etc. I am now building a new site on a subdomain on the same server for another client. I didn't want to create a new copy of the code, as they only wanted a few changes, so I am storing the name of the new client in the session when they log in, and add or remove code based on the client name.
This all works perfectly. However I just realised that one person could a be a user on my website as well as the new one.
How would I control this considering the fact that they could be logged in to both sites concurrently. I had considered having unique user names across both sites and storing the client name in the database against the user, but I don't want to do that because the number of clients is likely to grow, and I also might need to separate things later, if for instance someone wants to buy the current site.
Hope this makes sense. Any ideas most welcome.
Re: Multiple clients on a server
Posted: Thu Feb 25, 2010 11:52 am
by AbraCadaver
Look at session_set_cookie_params() using the path and/or domain args to control this. You can also set the various session.cookie_* vars using ini_set().
Re: Multiple clients on a server
Posted: Thu Feb 25, 2010 12:05 pm
by barb woolums
Thanks for your super quick reply AbraCadaver.
I checked out session_set_cookie_params() but I don't think it will work in this case because I am using lots of pages on my domain for the client rather than creating new copies of all the pages in their directory.
Re: Multiple clients on a server
Posted: Thu Feb 25, 2010 12:12 pm
by AbraCadaver
PHP is great for "dynamic" pages and sites.
Code: Select all
session_set_cookie_params(0, '/', $_SERVER['SERVER_NAME']);
session_start();
So when someone is on
http://www.example.com it will be a different cookie than subdomain.example.com.
Re: Multiple clients on a server
Posted: Thu Feb 25, 2010 12:26 pm
by barb woolums
OK I'll give it a try.
Very clever signature by the way

Re: Multiple clients on a server
Posted: Fri Feb 26, 2010 4:36 am
by barb woolums
I made a small mistake in my initial post. The client is actually on an add on domain, so their url points to a directory on my server. So $_SERVER['SERVER_NAME'] picks up the same domain.
Re: Multiple clients on a server
Posted: Fri Feb 26, 2010 6:08 am
by VladSun
Re: Multiple clients on a server
Posted: Fri Feb 26, 2010 9:05 am
by barb woolums
Thanks for that I now have two different session names, but when I use $_SESSION to add my user details, the previous values get overwritten.
I've looked everywhere, but can't find a way to link the session vars to the respective session name.
Re: Multiple clients on a server
Posted: Fri Feb 26, 2010 10:48 am
by AbraCadaver
You want the session name to be unique for the directory:
Code: Select all
session_name('PHPSESSID'.dirname($_SERVER['PHP_SELF']));
session_start();
Re: Multiple clients on a server
Posted: Fri Feb 26, 2010 12:59 pm
by barb woolums
I just can't get my head around this at all
I already have a unique session name. I just don't know how I keep the user/client details unique as well.
Re: Multiple clients on a server
Posted: Fri Feb 26, 2010 5:02 pm
by VladSun
barb woolums wrote:I've looked everywhere, but can't find a way to link the session vars to the respective session name.
You have to call session_name() before session_start() - everywhere.
Re: Multiple clients on a server
Posted: Fri Feb 26, 2010 6:06 pm
by AbraCadaver
VladSun wrote:barb woolums wrote:I've looked everywhere, but can't find a way to link the session vars to the respective session name.
You have to call session_name() before session_start() - everywhere.
Yes, an hopefully you have 1 session_start() call that you include from a header file or something.
Re: Multiple clients on a server
Posted: Sat Feb 27, 2010 12:52 am
by barb woolums
Yes I have session_start() in a header file.
I assume I would have to define the session name as in
session_name(1) or session_name(2) depending on which client i was logged in as?
In which case I have to carry the session name across pages?