Using Environment Variables

How to get some extra information
home    CGI Resources  |  Perl Tutorials

Home/CGI/Perl/Environment Variables

Still another way to get input is through the use of environment variables. These are set outside the program, but can be read in pretty easily. For example, let's say we want to read the value of the Query String in the url (the part after the ? in a url). Let's say the url was:
You could grab the "cool" bit off the url and assign it to a variable like this:

$iscool= $ENV{QUERY_STRING};

Using the $ENV{NAME} command, we can do the same to get other environment variables. We just replace the NAME with what we want to get, so:

$userip= $ENV{REMOTE_ADDR};

This will get the IP address of the user, which is useful in tracking a count for your unique visitors. Some other useful environment variables are listed below:

Name What it Gets
CONTENT_LENGTH Number of characters submitted from a POST command
HTTP_REFERER URL user came from, if available.
HTTP_USER_AGENT Type of Browser viewer is using, if available.
QUERY_STRING String in URL after the ? character
REMOTE_ADDR Remote user's IP address
REMOTE_HOST Name of user's host, if available.
SERVER_NAME Name of the local server, like:

Well, that's all for now, see you when I write the next section!

The tutorials and articles on these pages are © 1997-2003 by John Pollock and may not be reposted without written permission from the author, and may not be reprinted for profit.

By: John Pollock

Main Page  |  HTML  |  JavaScript  |  Graphics  |  DHTML/Style Sheets  |  ASP/PHP
PutWeb/FTP  |  CGI/Perl  |  Promotion  |  Java  |  Design Articles
Support Forums  |  Site Search  |  FAQs  |  Privacy  |  Contact

Copyright © 1997-2003 The Web Design Resource. All rights reserved.