How to Read the
Value of an HTTP Header

In
the above sections, we have gone through several HTTP headers that
are useful for user agent detection and device capabilities
detection. Now one essential question remains: how to read the value
of an HTTP header?

Reading
the value of an HTTP header is not difficult. Just use a server-side
scripting technology to write a few lines of code. We will
demonstrate how to read the value of an HTTP header using ASP, Java
Servlet / JSP, Perl and PHP below.

Retrieving HTTP
Headers with ASP

In
ASP, you can use the ServerVariables collection of the Request
object to retrieve the value of HTTP headers. You can choose
either VBScript or JScript (JavaScript implemented by Microsoft) as
the scripting language. If you use VBScript, the code for reading
HTTP headers should be like this:

As
you can see above, to retrieve the value of an HTTP header in
ASP, we use Request.ServerVariables("HTTP_x"),
where x is the HTTP
header name with all the "-" characters replaced
with the "_" character. ASP has other pre-defined server
environment variables that can be placed inside the parentheses of
ServerVariables() but we are
not going to discuss about them since they are not useful to us here.

If
you use JScript, the code for reading HTTP headers should look like
this:

As
you can see, to retrieve the value of an HTTP header whose name is x
in Java Servlet or JSP, we use request.getHeader("x"),
where request is an
instance of the javax.servlet.http.HttpServletRequest
class.

Retrieving HTTP
Headers with Perl

In
Perl, the values of HTTP
headers are stored in the %ENV hash. Here is the code for
retrieving HTTP headers:

As
you can see above, to retrieve the value of an HTTP header in
Perl, we use $ENV{"HTTP_x"},
where x is the HTTP
header name with all the "-" characters replaced
with the "_" character. Perl has other pre-defined values
that can be placed inside the braces of $ENV{}but
we are not going to discuss about them since they are not useful to
us here.

Retrieving HTTP
Headers with PHP

In
PHP, the value of HTTP
headers are stored in the $_SERVER array. Here is the code for
retrieving HTTP headers:

As
you can see above, to retrieve the value of an HTTP header in
PHP, we use $_SERVER["HTTP_x"],
where x is the HTTP
header name with all the "-" characters replaced
with the "_" character. PHP has other pre-defined values
that can be placed inside the square brackets of $_SERVER[]but we are not going to discuss
about them since they are not useful to us here.

What do you think about this web page? It is very helpful. It is helpful, but some information I wanted is missing. It is not helpful. It has broken links. It has typos / grammatical mistakes. It has incorrect information. Others

(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Due to the amount of messages we received, we may not be able to reply to all messages.