#!/usr/bin/perl -w
#
use CGI;
#
$browser = $ENV{'HTTP_USER_AGENT'};
$host = $ENV{'REMOTE_HOST'};
$ip = $ENV{'REMOTE_ADDR'};
#
print "Content-type: text/html\n\n";
#
print "$browser<br>\n";
print "$host<br>\n";
print "$ip<br>\n";
#
exit;
#
output:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;)123.65.43.21
Here is cgi a script that I use to print server stuff.
#!/usr/bin/perl -T
## printenv -- print CGI environment and perl @INC
print "Content-type: text/plain\n\n";
print "Environment variables:\n\n";
foreach $var (sort(keys(%ENV))) {
$val = $ENV{$var};
$val =~ s¦\n¦\\n¦g;
$val =~ s¦"¦\\"¦g;
print "${var}=\"${val}\"\n";
}
print "\n\nPerl \@INC:\n\n";
foreach $var (@INC) {
print "$var\n";
}
See [httpd.apache.org...]
Anyway, I found a new set of codes to extract the "Remote Host" using a different method. I'm not sure what it does, but it works and that's what matters.
$ip = $ENV{'REMOTE_ADDR'};
@numbers = split(/\./, $ip);
$ip_number = pack("C4", @numbers);
($host) = (gethostbyaddr($ip_number, 2))[0];
As as aside, I advise against doing this. Performing DNS lookups is *such* a performance hit, they're generally disabled on the webserver (that's why you're getting the IP and not the hostname in your script). DNS lookups can come back in fractions of a second, or take multiple seconds to run; do you really want to incur that kind of performance penalty for a request to your site?
I run webservers for a living; if I found out that one of my developers wrote a script that did a DNS lookup on each request, the first thing I would do is disable the script. The second thing I would do is take the developer (and a large stick) out behind the woodshed for a stern talking to. =)
This script I'm doing has two functions. One acts as a counter, extracting data from the users and stores them in a pipe delimited .db file. When a visitor calls this file, It'll extract relevant data (including hostname) and returns a tiny gif image.
The other function is basically just to display the data to me in a tabled format.
So, will this still be a "hit" in performance although it's just a small gif image?
1) Your local nameserver has the ip -> host mapping cached already. This will happen when that nameserver has already had to resolve that IP address. In those cases, the only speed limitations are the ones imposed by the network between your webserver and the resolver, and the load under which the resolver is currently operating.
2) If your local nameserver does /not/ have that IP -> hostname data cached, it needs to run out and find it. This can be near-instantanous, or take a while, depending on the IP address. Results can (literally) take fractions of a second:
$ time host www.webmasterworld.com
www.webmasterworld.com has address 64.33.51.156
real 0m0.018s
user 0m0.000s
sys 0m0.003s
...or quite a bit longer:
$ time host www.example.co.uk
www.example.co.uk has address 212.111.222.33
real 0m1.036s
user 0m0.001s
sys 0m0.002s
3) The IP couldn't be looked up for some reason. This can happen because a) the IP has no reverse DNS or b) the resolver ultimately responsible for that IP -> hostname mapping is unreachable for some reason (network outage, power outage, server issues, etc). In these cases, things can take MUCH longer:
$ time host www.example.com resolver.example.net
;; connection timed out; no servers could be reached
real 0m10.063s
user 0m0.000s
sys 0m0.003s
That's 10 seconds during which a) your webserver is tied up, waiting for a response that's never going to happen (which means that child process or thread can't do anything else, reducing your capacity) and b) your user is sitting there waiting for a response.
The data is stored in a pipe delimited format:
data1¦data2¦data3¦...¦IpData¦HostNameData¦...¦... etc...
If I'm able to make it such that, when a visitor call for it, it'll skip the "hostname-retrieval" process. And when I call for it, it'll scan through the data, find out if the hostname data is empty, then perform the retrieval, and reinsert the data into that blank.
This way, it'll solve the problem of slow responce to my visitors. And i'll still have them in my logs.
Complicated process I would say. Don't have the time, energy nor resources to work on it at the moment.
Thanks very much for your help. Sitz