Multiple clients on a server

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
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Multiple clients on a server

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Multiple clients on a server

Post 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().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Multiple clients on a server

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Multiple clients on a server

Post by AbraCadaver »

PHP is great for "dynamic" pages and sites. 8)

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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Multiple clients on a server

Post by barb woolums »

OK I'll give it a try.

Very clever signature by the way :)
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Multiple clients on a server

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Multiple clients on a server

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Multiple clients on a server

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Multiple clients on a server

Post 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();
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Multiple clients on a server

Post by barb woolums »

I just can't get my head around this at all :crazy:

I already have a unique session name. I just don't know how I keep the user/client details unique as well.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Multiple clients on a server

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Multiple clients on a server

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Multiple clients on a server

Post 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?
Post Reply