简体   繁体   中英

Multi Tenancy Multiple Client Multiple Database, One Source Code - PHP

I have a task to create an existing project to multi tenancy, as the application has many clients so we decided to do this.

We have merged our source code to work with any database, all source code are same for all client.

Now, earlier the database connection has happened using one config file includes in each script.

but, as you know the source is now having multiple client which databases are different, we are stuck to manage the DB with the client.

Intially,

http://localhost/workspace/client/ --> this is a URL given to client, now I redirect this to index.php using htaccess and based on client/ I knew which client this package is going to use.

there are different clients, http://localhost/workspace/clien1/ http://localhost/workspace/clien2/ http://localhost/workspace/client3/

and so on..

Now, in index.php page redirect to login.php, but the config is not set for the DB as per the client.

if anyone handled this, please help me.

Thanks in advance.

The database connection parameters (server address, db username / password, and database name) are, in your proposed setup, attributes of your users or your customers (your users' organizations).

I guess you will authenticate your users by looking them up in a common database and then verifying passwords with password_verify() When you know you have a valid user you can

  • retrieve the db connection parameters from your user database
  • store them in php session variables
  • use php's secure session cookie scheme to leave your user's browser with a way to identify the appropriate session.
  • after redirection, or on subsequent web requests, open the db mentioned in the session variables

Make sure the cookie you place doesn't contain the database parameters embedded in it, or a cybercriminal will figure out how to change the cookie to gain unauthorized access.

But, Pro tip: Creating a new database for every customer is famously unscalable. What happens if

  • your project is successful and you find yourself adding a thousand new customers some day? You will have to add a thousand databases that day.
  • you have 500 active sessions at some moment? Each of them will have its own dbms connection, making php's connection pooling schemes useless. Connection pooling is vital to good web application performance.
  • you have a need to perform some reporting operation that covers all customers? You'll have to run the reports for each customer separately and figure out how to merge them together in your report program. That is difficult.

A good practice for multitenant online applications is to put customer id or user id values on each data record, and use query clauses like WHERE customer_id = ? (current customer) to separate data user by user.

Hopefully it is not too late to revisit your design decision to use multiple databases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM