ayapejian has asked for the wisdom of the Perl Monks concerning the following question:

So I'm developing a backend CGI script that several virtual hosts will need to hit. Depending on which virtual host is accessing the file they will need different settings set ( database names, id's, etc.. ). My question is this, what is the best way to tell what virtual host is accessing the file? I guess I could go by the referer for the CGI object, however I don't know how subdomains (which should not come into play) would effect this for instance:
http://www.yahoo.com http://yahoo.com http://someplace.yahoo.com/blah_blah/index.html
The above should all return 'yahoo'. After I work this part out I can store the values in a configuration file that just contains a hash for each host I guess. Any ideas, or alternate suggestions are welcome. Thanks in advance.

Replies are listed 'Best First'.
Re: Telling different virtual hosts apart
by Fletch (Bishop) on Apr 29, 2008 at 01:03 UTC

    Takes 2 seconds to pull up the documentation for the CGI module and search for "virtual" . . .

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Telling different virtual hosts apart
by sasdrtx (Friar) on Apr 29, 2008 at 01:55 UTC

    While you could do as Fletch implied, and look up the virtual host in the environment; another way is to module-lize your CGI code, and invoke it with a different script for each vhost. The script would just invoke some routine in the module, passing it whatever customized parameters are needed for that vhost. This is easier if you have separate document-root directories for each vhost, but a common module directory.

    sas
Re: Telling different virtual hosts apart
by pc88mxer (Vicar) on Apr 29, 2008 at 03:34 UTC
    I have a couple of questions... First, based on what you say here:
    So I'm developing a backend CGI script that several virtual hosts will need to hit.
    it seems that your script is NOT running on a virtual host but is being accessed (via HTTP) by other virtual hosts, and the behavior of your script is dependent on what virtual host is making the call. Is that correct?

    Secondly, when you write:

    I guess I could go by the referer for the CGI object
    do you mean $ENV{HTTP_REFERER} or $ENV{REMOTE_HOST}?

    In any case, my approach would be to develop a small rule engine that would allow your customize it for subdomains if you ever needed to:

    my %domain_map; $domain_map{'yahoo.com'} = ...default setting for all of yahoo.com... $domain_map{'special.yahoo.com'} = ...special setting for special.yaho +o.com... sub find_setting { my $domain = shift; # either from $ENV{HTTP_REFERER} or $ENV{REMOTE +_HOST} my $setting; while (length($domain)) { defined($setting = $domain_map{$domain}) && last; $domain =~ s/^(.*?)(\.|\z)//; } return $setting; }
Re: Telling different virtual hosts apart
by aquarium (Curate) on Apr 29, 2008 at 02:21 UTC
    another way to think about it:
    if the script works with different databases etc...looks like they do a different job, and should be separate scripts. Even if they do share a lot of functionality, the common parts can be made modular.
    yet another way...
    if the jobs are the same, then the virtual hosts (or sub-functions thereof) should become standardized and use same database name etc. if you split up and gave different names to databases just for scalability...there are different ways to approach that problem
    sometimes...we need to step back a bit to see a better solution at higher level...just sometimes
    cheers, and good luck.
    the hardest line to type correctly is: stty erase ^H