r/PHPhelp • u/Prestigiouspite • Nov 14 '25
How Would You Architect Multi-Tenant DB Mapping for a PHP/CodeIgniter SaaS Without Subdomains?
I’m building a SaaS product in PHP using CodeIgniter for my own companies and I’m now considering offering it to external clients as well. Since the application handles sensitive business data, I’m leaning toward giving each tenant its own dedicated database rather than relying on a shared schema with a tenant ID. The risk of cross-tenant leakage due to a forgotten condition in a query is something I want to eliminate as much as reasonably possible.
I briefly considered isolating every tenant in its own container, but the operational overhead feels excessive for this use case. It’s not a financial or compliance-heavy product, so full container-level isolation would likely add more complexity than value.
The main question I’m trying to solve now is: what’s the most sensible way to map a tenant to the correct database? The straightforward solution would be to use subdomains and switch the DB connection based on the subdomain, but I don’t really like the UX of that approach. Ideally, I want a single unified login URL where all users sign in with their credentials and are then routed to the correct tenant space.
The complication is that all login data is stored inside each tenant’s database. I also don’t want to add a third login field like “Tenant ID” just to know which database to connect to. So I’m wondering how others approach this. How do multi-tenant accounting solutions and similar SaaS tools handle this when they also don’t use subdomains?
Curious to hear how you would design this and what patterns you’ve seen work best for securely routing logins to the right tenant database without compromising UX.
3
u/LostInCyberSpace-404 Nov 15 '25
So I have something similar for a saas product using the same software stack. I have a master db and then each tenant has a db. When users are created, edited or deleted they also get the same changes pushed to the master db. In the master db In my users table I have login_id and tenant_id where login_id is their email. When they login it does a lookup using that data. If the same user exists in multiple tenants they are prompted to select the tenant.
2
u/Ok_Sand_9039 Nov 15 '25
Im your central db make a pivot table between users and tenants, when the user logs in you fetch their tenant and switch the connection to the tenant database (that if the user can only have a single tenant) in case of multiple you add active_tenant_id to decide which tenant you switch to.
2
u/FreeLogicGate Nov 16 '25
For future reference, you tend to get more informed results if you specify the database you're using. There are important differences between them.
I think your first challenge is determining how you will handle identity and authentication. You are jumping right to a database design without considering session at all.
In the world of Identity and single sign on etc, there are a variety of tried and true solutions, which have different use cases and wheel houses -- things like Kerberus, LDAP, Oauth2 and Radius come to mind.
The standard that looks to me to be the most applicable to your use cases, is SAML. I'd suggest you do some reading about what SAML is and consider how it solves many problems for you. You can also compare it to the other technologies I listed. It can also be enhanced with SCIM which adds standards around provisioning, where you create a SCIM compliant end point.
Using SAML and perhaps SCIM will allow you to avoid all the issues involved in "roll your own authentication and security" which is needless to say rarely the best decision.
PHP has a long standing project you can base your code on named SimpleSamlPHP which has both and IDP and SP implementation available. There are also a good number of other FOSS IDP's like Authentik you could self host.
6
u/minn0w Nov 14 '25
If the tenant is a property of the user, the user table is global and not tenanted.
1
u/WebCodeLogic Nov 15 '25
Yeah you’re going to need a master db with a table to map users to a database. You can use a pivot table in the master database to map a database to a user from a specific database user table. Remember to use an email or something similar as a unique identifier. Users from different databases may have the same integer id from its users table. Just make sure you have a means of unique id so your algorithm doesn’t get confused.
1
u/SativaNL Nov 15 '25
You are making it yourself pretty complex. Why not just use a subdomain? Or even better. Think about why not use 1 database with a tenant ID for separation. I use this myself. A forgotten condition is something you must always remember that this is a high prio to check. But also, You can have AI so check the complete codebase. Just include the tenant ID everywhere in your functions to get data, and you will be safe.
1
u/mnavarrocarter Nov 15 '25
First, you need to think if you need your tenants connected in some way (for instance, a user belonging to multiple tenants). If that's not a requirement, I would recommend the approach you suggested (completely separate databases for each tenant).
Also, routing based on subdomain is the correct approach. However, is a massive security risk to let PHP handle this routing. For PHP to handle this routing it would need to have access to a repository of tenants and their respective database credentials (to do the switch). This means if someone hacks into your instance, they also hack all tenants data.
Moreover, above approach introduces massive complexity to your application booting since you can't really initialise anything in your services until your application knows the tenant (you'll be battling uphill against what's the main assumption in most frameworks).
I would handle the routing in some other layer, like a reverse proxy or API Gateway. That layer only will map the subdomain and obtain secrets and environment variables to pass them to the PHP FPM process. That way, the PHP application can run as is and without access to secret information of other tenants.
I built a Caddy plugin precisely for this purpose ages ago. Let me know if you need a hand installing it and trying it.
2
u/dutchman76 Nov 15 '25
If the login URL they use is also on the subdomain, then it's easy to figure out still.
Or have a central mapping table that tells you which user is on which sub
2
u/jim-chess Nov 16 '25
You could check out this package for multi-tenancy in Laravel: https://tenancyforlaravel.com/
It also helps with things you may not think of right away, like tenant aware queues and caching.
However since you mentioned *not* wanting to use subdomains, you could also just write some middleware which, upon the user signing in (via a centralized database with a users table), sets the DB connection dynamically. This would work if the user belongs to exactly one organization.
Just gotta be careful and make sure your cache, queues, and storage layer are fully separated as well. Test cases are definitely your friend here.
EDIT: Oops just saw you asked for CodeIgniter specifically. But I think the same general approach can still apply with the custom middleware.