環境変数出力cgiの一例

 

#!/usr/local/bin/perl

 

 

 

# 時間の取得

($sec,$min,$hour,$mday,$mon,$year,$wno) = localtime(time);

$time = sprintf(“(%01d/%01d %02d:%02d:%02d)”,$mon+1,$mday,$hour,$min,$sec);

 

# HOST_NAME ホスト名

$host_name = &GetHostName($ENV{‘REMOTE_ADDR’});

sub GetHostName {

my($ip_address) = @_;

my(@addr) = split(/\./, $ip_address);

my($packed_addr) = pack(“C4”, $addr[0], $addr[1], $addr[2], $addr[3]);

my($name, $aliases, $addrtype, $length, @addrs);

($name, $aliases, $addrtype, $length, @addrs) = gethostbyaddr($packed_addr, 2);

return $name;

}

 

# REMOTE_ADDR リモートIPアドレス

$addr = $ENV{‘REMOTE_ADDR’};

 

# HTTP_REFERER 参照元URL

$referer = $ENV{‘HTTP_REFERER’};

 

# HTTP_USER_AGENT ユーザエージェント

$user_agent = $ENV{‘HTTP_USER_AGENT’};

 

 

 

 

print “Content-type: text/html\n\n”;

print <<“EOM”;

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

<html lang=”ja”>

<head>

<META HTTP-EQUIV=”Content-type” CONTENT=”text/html; charset=Shift_JIS”>

<META HTTP-EQUIV=”Content-Style-Type” content=”text/css”>

<title>環境変数</title></head>

<body bgcolor=”#FFFFFF” text=”#000000″>

<br/>

<br/> [環境変数出力]

<br/>

<br/> <b>HOST_NAME</b>= <font color=”#e60044″><strong>$host_name</strong></font>

<br/>

<br/> <b>REMOTE_ADDR</b>= <font color=”#e60044″><strong>$addr</strong></font>

<br/>

<br/> <b>REMOTE_REFERER</b>= <font color=”#e60044″><strong>$referer</strong></font>

<br/>

<br/> <b>REMOTE_USER_AGENT</b>= <font color=”#e60044″><strong>$user_agent</strong></font>

<br/>

<br/> $time

</body>

</html>

EOM

exit;