• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
area.example.com - How to do Subdomains for Location-Based Database Filtering?

Hey All,

I'm new to CI and have gone through most of the CI video tutorials on the NetTuts website to familiarize myself somewhat with the framework.

Currently, I'm working on developing my own website that delivers location-based content to users. I need the content to be filtered similar to eBay Classifieds / Kijiji, where users will be directed to the area (subdomain) nearest them, after selecting their location.

I read the thread: How to manage subdomains? and it seems like this solution could work for me but, being new to CI, I'm not really sure about the implementation of such a solution, or if it would be better to use hooks or something?


An example of what I want to do:

- id
- name
- etc

- id
- location (eg. "chicago.il.usa", or "toronto.on.ca")
- type
- etc

- id
- location
- type
- etc

User selects Joliet, IL. They're redirected to chicago.il.example.com, showing them all the fruits available in the greater Chicago Area in which they reside.

I can handle the database work - I just want to know how (or where) would I implement getting "chicago.il" from the URL so that I can use it in my SQL query?

In the example above, browsing Fruits and Vegetables would be different pages of the site, with each pointing to yourArea.state.example.com/page, just like browsing Houses or Cars would be different on a classified website.


Another question I'm not too sure about is:

Would it be better (for performance) to make a "fruit" table for each area (chicagoFruit, torontoFruit), or to stick with a single "fruit" table and use url-style locations with "where location like 'chicago.il%'"?

I am expecting up to about 20,000 records in the database at any given time and site traffic of around 150K hits/day

You could probably combine your fruits table with 20k records. Or you could prefix your table names with the location and let the model decide which one to query based on the host header. Host header is really easy to get in PHP:

$host = $_SERVER['HTTP_HOST'];

I do not recommend allowing host header values into your queries. If your location base is large, than I recommend at least keeping a global array with all the possible combination and checking against it or going with a default location (or detect it again.)

[quote author="bretticus" date="1281529194"]I do not recommend allowing host header values into your queries. If your location base is large, than I recommend at least keeping a global array with all the possible combination and checking against it or going with a default location (or detect it again.)[/quote]
I won't be placing the location directly into the query - I'll be verifying their location against an array of acceptable cities. If it's valid, then I'll load the information from the db. If not, I'll use geolocation to find the nearest acceptable location then redirect them and load the info from the db.

I was wondering if I should use the $_SERVER['HTTP_HOST'] method to get the subdomain or if there was a better way to get it, but that makes sense.

Thanks for your help Smile

If you want to "keep it in the framework"... You can use the server method of the input class:

$host = $this->input->server('HTTP_HOST');

If I were doing your project this is how I'd go about it:

First, I'd set up a wildcard in DNS that points to my IP.

Example of bind zone file:

ORIGIN example.com.
*  A

In your Apache config file under your virtual host:

ServerAlias *.example.com

Now, your only problem is when you use any framework method/function that builds URL's. Fortunately, you can extend CI via a MY_Config library. The method that all these URL building functions ultimately reference is site_url(). I'd probably look at the host header here and check it against that array. You can even modify this function to look at your array list because you are extending the CI_Config class so config items can be referenced as $this->item('name') in your MY_Config class. If I couldn't find a match, I'd go with a default again. That way your links will always reference an acceptable URI.

Good luck. Sounds like a fun project!

It looks like godaddy doesn't allow wildcard subdomains for static IPs Sad

So now I've got to decide if I want to do it this way, and spend an extra few bux a month, or if I want to use the URI method and do it for free.

I think I prefer this method because it seems to align more easily to what I want to do (I can just grab the city from the subdomain, as you said, by using the server('HTTP_HOST') method.

You can still set up all your subdomains manually and point them all (set them to the same static IP) to your website. Implementing wildcard sub-domains is just a shortcut (that allows you greater flexibility and less hassle in the future.) Doesn't make a difference as long as your website is configured to answer when it encounters your sub-domain host headers. In a pure apache config this is what ServerAlias is for. I don't think you can get that hands on with godaddy, but I know you can at least create the sub-domains and point them. If they (godaddy) have a sub-domain limit, I suggest finding a new host (preferably one that allows wildcard sub-domains.)

I don't see the decision really. Basically it's just sending all the sub-domains to the same virtual host. You then alter your models logic for each sub-domain encountered (as to get the associated data for that sub-domain.) Again, that is easy to detect via host header as demonstrated. Then, if you want to use (you don't have to) you can extend site_url() to make sure it matches the current sub-domain.

Yeah - and using the subdomain method is the method I think I really wanna do, because the URI method seems like it'd be a lot more complicated, especially since my site will have a variable number of parameters for the method calls.

I just might take you up on the suggestion to find another provider. I called GoDaddy, and they said that I had to make the configuration to allow wildcard subdomains, to make it point to my server on the shared hosting space (which, I don't think he knows, requires httpd.conf access).


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.