Using Environment Variables
How to get some extra information
http://yoursite.com/cgi-bin/testenv.cgi?cool
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: www.yoursite.com |
For more on environment variables, visit CGI 101, Chapter 3 for an excellent listing and tutorial.
Well, that's all for now, see you when I write the next section!
