Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

to $Env{'HTTP_HOST'} or to $Env{'SERVER_NAME'}, that is the question.

by JK_Bean (Novice)
on Jun 28, 2001 at 19:37 UTC ( [id://92325]=perlquestion: print w/replies, xml ) Need Help??

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

When checking $Env{}, which is the proper key to use to see if the domain is www.myserver.com or myserver.com, or is there another more technically correct way do to this?

When I use either 'HTTP_HOST' or 'SERVER_NAME', I get the same result. I want to be safe so my code is more portable.

  • Comment on to $Env{'HTTP_HOST'} or to $Env{'SERVER_NAME'}, that is the question.

Replies are listed 'Best First'.
(dkubb) Re: (2) to $Env{'HTTP_HOST'} or to $Env{'SERVER_NAME'}, that is the question.
by dkubb (Deacon) on Jun 28, 2001 at 20:29 UTC

    IMHO, the best way to get at that information is through CGI.pm's method virtual_host():

    use CGI; my $cgi = CGI->new; my $domain = $cgi->virtual_host;

    It will attempt to Do The Right Thing in determing the virtual host's name. I believe it is cleaner, and more portable to use CGI's methods to access the CGI specific environment variables, than to access them directly by hand.

      In reality, all that's going to do is give you $ENV{"HTTP_HOST"} (or $ENV{"SERVER_NAME"} in the highly unlikely event of the Host: request header being specified)

      The Host header (read $ENV{"HTTP_HOST"}) which is manditory for HTTP/1.1 (and IIRC also for HTTP/1.0) specifies the host name for the request; this was designed specifically for "virtual hosting".

      If you already have a CGI object floating about, then I'd recommend using $cgi->virtual_host. If you don't, it's probably easier to just use $ENV{"HTTP_HOST"}

      --
      RatArsed

        Perhaps something has changed in all these years: using PERL 5.14 and Apache virtual hosting all of these gives you a different result:

        $CGI->virtual_host() returns DNS name ex. www.my.address.com $ENV{'SERVER_NAME'} returns "localhost" $ENV{'HTTP_HOST'} returns "localhost:8001"

        The actual values of course depends on you environment setting.

Re: to $Env{'HTTP_HOST'} or to $Env{'SERVER_NAME'}, that is the question.
by tune (Curate) on Jun 28, 2001 at 19:57 UTC
    I do not get the same. HTTP_HOST gives the same server name as I typed in the Location box of my browser. It can be 'localhost' for example.
    SERVER_NAME is the same everytime, hostname + domain, as it is registered in the machine's configuration.

    --
    tune

Re: to $Env{'HTTP_HOST'} or to $Env{'SERVER_NAME'}, that is the question.
by wardk (Deacon) on Jun 28, 2001 at 20:45 UTC

    JK_Bean, just a heads up...you should be using $ENV{'HTTP_HOST'}, i.e. %Env should be %ENV.

      yes, you are right. that was a typo. my code does in fact use $ENV, all caps.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://92325]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-20 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found