I have written a basic web app in php and MySQL that uses query strings to look up various information. For example: xxxxxxx.com/?profile=1245
Very basic, first time developer type of web app.
I want to create different accounts that will each have their own sets of data. I want to differentiate these accounts by using subdomains. For example: username.xxxxxxxx.com?profile=4527
I have a catchall set up in my Apache config to divert all subdomains to my main domain and then I can pull that subdomain out using php like so:
<?php list( $username ) = explode( ".", $_SERVER[ "HTTP_HOST" ] ); ?>
Now, HERE’S MY QUESTION:
It seems to me (I’m fairly new to all of this) that performing a string based search for every query I need to do on every page is going to be slow and inefficient. For instance, having to look up SELECT * WHERE username = sampleuser AND profile = 2745
But is that true? What would be the most efficient way? How do web apps that use subdomains normally handle this sort of thing?
Thank you all so much in advance.
“How do web apps that use subdomains normally handle this sort of thing?”
Usually each subdomain is mapped in the web engine(apache/IIS) using something like rewrite module, to a specific IP address or a specific root. So blog.* subdomain might point to the root of the WordPress app.
In your case, that sounds like it wouldn’t apply unless you have a different set of pages for each user. I.e. if they all have a “Account Details” page, and only the data is different, then the only place the subdomain would come into play is with queries, or URLs you generate.
In your case, you have what are conceptually dynamic routes. Usually you would just query a table to determine what content to serve. In your case it’s not so much content as just data. Sometimes the routes are cached globally in memory using a data structure that is well organized for lookups, in your case lookups via the username that give you back the primary key of the users table. Then without querying the user’s table, you have your filter criteria for data that references that user via a foreign key. So if the navigate to the “Likes” page, you can “Select * From Likes Where UserKey = $userKeyFromLookup”. You would want to choose an in memory data structure for lookups that allows O(1) lookups. I.e. constant time lookups.
Is profile ID unique to each user? If so, then you don’t need username in the query. The profile ID alone would give you a unique response. If someone visits username.. without a profile ID, then first thing you do is grab the profileID then redirect to the same page but now with that parameter in the URL so that all other navigation is done in the context of that profileID.
I agree with Stony though, I think you will find quarks with having dynamic subdomains. I would put the username in the URL instead.