[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [cobalt-users] Getting ServerName or domain name in script



Hello Cody,

> And since this script is being used for multiple domains 
> all at once, is there a way it can find out which domain it 
> is being used 
> under. It can probably get it from the HTTP Header, but how 
> do I extract 
> it, and how do I isolate just the domain name?

To find out what server you are running on can be quite tricky as not all
servers are equal when it comes to the Environment Variables that they
return. However, on the RaQ3 the variables are quite complete and you can
use the (reasonably) standard HTTP_HOST environment variable like this:

#!/usr/bin/perl
# Isolate this server
$this_server = %ENV{HTTP_HOST};
# Display it in the users browser to prove that we have it (you don't need
this next bit)
print "content-type: text/html\n\n";
print $this_server;
exit;

The variable $this_server will now have the name of the host as defined in
that virtual hosts  Apache setting for ServerName. So, for our domain
$this_server would be www.waytotheweb.com.

Out of interest, to find out what is in all of the environment variables is
easy, like this:

#!/usr/bin/perl
print "content-type: text/html\n\n";
print "<table border=1>\n";
for $key (sort keys %ENV) {
   print "<tr><td align=right>$key<td>$ENV{$key}</tr>\n";
}
print "</table>";
exit;

Regards,
Jonathan Michaelson

Way to the Web Ltd
Commercial CGI Scripting, Web Hosting
http://www.waytotheweb.com