Part of the EllisLab Network
   
1 of 2
1
Username based URL help needed
Posted: 03 May 2007 09:37 AM   [ Ignore ]  
Summer Student
Total Posts:  10
Joined  01-30-2007

I have a project I’m beginning work on and have a few specific requirements that I am not sure how to go about.  First of all, there will be many clients, each with their own domain name in the following format:

www.username.domain.com

Overall, the site will look exactly the same except for the initial page which would have the clients contact information, picture, etc.  Also, there will be a few forms spread about the site and I would like to associate those submissions to that particular client, based on client_id or something.

In my head, I would think you could just point the subdomain to the base code. Then from there, explode the url to get the username and pull appropriate data from the db.  But, how would you deal with $config[‘base_url’] in the config file?


You guys have any ideas or perhaps a better means of doing this?

Thanks so much,
-phear

Profile
 
 
Posted: 03 May 2007 10:09 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  10
Joined  01-30-2007

It appears I can input $_SERVER[“HTTP_HOST”] as the variable for $config[‘base_url’].

From there I can perform the following:

$url = strip_tags(base_url());
$url_array = explode('.',$url);

This would allow me to run queries, initialized session variables, etc using the username as the discriminator.

Do you guys foresee any problems doing this?

Profile
 
 
Posted: 03 May 2007 11:45 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  248
Joined  02-10-2007

I once constructed a site that made use of dynamic subdomains. I went about almost exactly the way you are doing it right name. Here’s a snippet of the config file of that site.

// example HTTP_HOST: "sub.domain.com"
$host = explode('.', $_SERVER['HTTP_HOST'], 2);
define('TLD',       $host[1]); // contains "domain.com"
define('SUBDOMAIN', $host[0]); // contains "sub"
 Signature 

Kohana rocks!

Profile
 
 
Posted: 29 April 2009 03:56 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  48
Joined  03-21-2007

The following snippet fixes the constants when you dont have a subdomain at all (e.g. domain.com, because without this you get domaincom as the TLD… not exactly correct)

Enjoy, and thanks for getting the ball rolling in my head for this!

if(count(explode('.', $_SERVER['HTTP_HOST'])) > 2) {
    $host
= explode('.', $_SERVER['HTTP_HOST'], 2);
    
define('TLD',       $host[1]); // contains "domain.com"
    
define('SUBDOMAIN', $host[0]); // contains "sub"
} else {
    define
('TLD',        $_SERVER['HTTP_HOST']);
    
define('SUBDOMAIN', "");
}
Profile
 
 
Posted: 29 April 2009 09:26 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  467
Joined  03-21-2009

The only downside is that creating a new account would require a waiting period as the new subdomain would need to propagate to all of the DNS servers of the interweb.  That’s why most sites use the ‘www.domain.com/username’ format.  In that same vein, browsing users may be a few milliseconds slower as each profile view now requires the browser to hit up DNS.

Profile
 
 
Posted: 30 April 2009 06:23 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  48
Joined  03-21-2007
slowgary - 29 April 2009 09:26 PM

The only downside is that creating a new account would require a waiting period as the new subdomain would need to propagate to all of the DNS servers of the interweb.  That’s why most sites use the ‘www.domain.com/username’ format.  In that same vein, browsing users may be a few milliseconds slower as each profile view now requires the browser to hit up DNS.

Actually, no. Wildcard DNS prevents this. I can type in any subdomain and it is already resolved.

Profile
 
 
Posted: 30 April 2009 06:46 AM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  467
Joined  03-21-2009

But the browser would still have to make the DNS request, wouldn’t it?

Profile
 
 
Posted: 30 April 2009 08:16 AM   [ Ignore ]   [ # 7 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3621
Joined  04-25-2008

I think that what brendanc is saying, is that you don’t need to wait for subdomains to start resolving. All subdomains resolve the the same server.

Also, on a side note, the browser may not have to make a DNS request, as I believe Windows at least, caches DNS calls.

 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
Posted: 30 April 2009 10:39 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  48
Joined  03-21-2007

Mac OS X also caches DNS calls, and they can be cleared with (on Leopard anyway)

dscacheutil -flushcache
Profile
 
 
Posted: 30 April 2009 01:56 PM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  467
Joined  03-21-2009

Windows does cache DNS calls (which can be cleared using ipconfig /flushdns).  BUT… doesn’t a new subdomain require a new DNS request as it’s basically like a new domain name?  If not, the idea of “subdomains take time to propagate DNS” wouldn’t make sense.  I *think* using subdomains like this would basically cause the local DNS cache to be useless for those URLs.

Profile
 
 
Posted: 30 April 2009 08:31 PM   [ Ignore ]   [ # 10 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3621
Joined  04-25-2008

Yes, of course the DNS is needed for the initial request, but with wildcard DNS, once that’s propagated, that’s it. All requests to any domain name will be forwarded to the same server, and the local DNS should sort out the rest. When a new subdomain is added, no propagation is necessary, only the local DNS needs to know where to map the subdomains. I thought that was the whole point of what brendanc was saying?

 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
Posted: 01 May 2009 04:46 PM   [ Ignore ]   [ # 11 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  467
Joined  03-21-2009

So the local machine will actually cache the wildcard?  In windows you can manually add DNS info in the hosts file, but it doesn’t support wildcards.  I though the local cache would be similar.  For instance, no matter which user you lookup, your ISP’s DNS server returns the IP for whatever the server is, but when the local machine caches it, it doesn’t cache an entry like this:

*.domain.com       235.123.523.21

It cache’s the static version, which is the actual request that was made:

jimsmith.domain.com       235.123.523.21

Which means each request to a new subdomain would still require the browser to hit up the DNS server.


I’m not really sure about this, I just thought that’s how it worked.  Can anyone confirm?

Profile
 
 
Posted: 01 May 2009 10:04 PM   [ Ignore ]   [ # 12 ]  
Grad Student
Rank
Total Posts:  48
Joined  03-21-2007

You may be right about needing to make a DNS request, but I would think… in the case of having a wildcard DNS entry, that even if you have to make a request, the lookup will be so fast that it won’t matter (matter of milliseconds).

Either way, I didn’t mean to derail a 2 year old thread into talking about DNS caching raspberry I was just adding my little snippet of code to handle not having a subdomain included.

Cheers

Profile
 
 
Posted: 02 May 2009 04:49 AM   [ Ignore ]   [ # 13 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3621
Joined  04-25-2008

No, you can’t cache a wildcard domain… brandanc’s point is that you don’t need to wait for any specific domain name to propagate, I don’t know why you seem to have trouble letting go of the whole DNS query thing…

And yes, a new request needs to be made for each subdomain. The browser is oblivious to the fact that the the subdomain is a wildcard - only the DNS servers know.

Try these URLS.

http://something.codeigniter.com/
http://something_else.codeigniter.com/
http://another_thing.codeigniter.com/
http://whatever.codeigniter.com/

codeigniter.com clearly has wildcard DNS enabled, or do you really think they added those 4 subdomains? Try it yourself with any subdomain of your choice. If it’s not in use, it will direct you to the codeigniter.com Web site.

Now, let’s say we wanted to add http://slowgary.codeigniter.com/. We can! It already resolves, we just need to configure the local name server to send that request to whatever Web root you’ve been designated.

Windows cannot cache wildcard subdomains. Instead, it caches them by the name that you requested. It’s the DNS servers that actually store the wildcard DNS setting.

If you want to know more about wildcard DNS, please see this wikipedia article.

 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
Posted: 02 May 2009 08:33 AM   [ Ignore ]   [ # 14 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  467
Joined  03-21-2009

HA!  I hadn’t realized it was such an old thread or I wouldn’t have responded in the first place.

I understand that DNS propagation will not be an issue, and I don’t have an issue with the non-cache-able DNS info, I just wanted to make sure I (and any readers) understand the pros and cons of this approach.  I personally don’t think trading local DNS caching for subdomain user accounts is worthwhile and I’m sure ISP would agree.

Profile
 
 
Posted: 02 May 2009 11:19 AM   [ Ignore ]   [ # 15 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3621
Joined  04-25-2008

Sorry, I don’t understand the trade off. Whether a named subdomain is set up, or wildcard DNS, it’s still only one DNS call. I think wildcard DNS is a Godsend. The number of times I configured my subdomain wrongly and had to wait 8-24 hours to find out is ridiculous. With wildcard DNS, my subdomains start resolving within 10 minutes.

 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77466 Total Logged-in Users: 16
Total Topics: 101508 Total Anonymous Users: 2
Total Replies: 544249 Total Guests: 187
Total Posts: 645757    
Members ( View Memberlist )
Active Members:    alijanahandriodAnimeCYCaztackbagwaaCrucialDan BowlingDennis_gulldepthchargedomijayrulezKunturNateLnayaabskeletonfriendspmckee