#! /usr/local/bin/perl # # クッキーテスト # # id=cookie.cgi # # update=97.7.19 # 日本語コード変換ライブラリ(v2.0)の指定 # (このCGIと同じディレクトリに設置する場合は、'./jcode.pl'でよい) # &ReadParse命令で、$in{ }の変数にフォーム入力データが格納される require 'cgi-lib.pl'; require 'jcode.pl'; &ReadParse; # 設置するURLを設定 $reload = 'http://www.kumagaya.or.jp/cgi-bin/users/chichibu/cookie.cgi'; $bkhmpg = "http://www.kumagaya.or.jp/~chichibu/cgi.html"; $cookie_name = 'cookie_test'; #################################### # # # メインルーチン(処理の分岐) # # # #################################### # 全体の流れを決定する(actionやpwdはフォーム入力されたデータを格納する名前) # action=check --> フォーム入力データのチェック処理へ # その他 --> フォーム入力画面へ if ($in{'action'} eq 'first') { &html; } elsif ($in{'action'} eq 'check') { ✓ &first; } else { &first; } ############################################# # # # 最初に表示する画面 # # # ############################################# sub first { print "Content-type: text/html\n\n"; print '
' . "\n"; } #################################### # # # フォーム入力画面出力ルーチン # # # #################################### sub html { # 環境変数HTTP_COOKIEにキーワードが含まれているか調べる if ($ENV{'HTTP_COOKIE'} =~ /$cookie_name/) { # 含まれていれば、それを処理する &cookieCutter; } else { # そうでなければ、初めての訪問とみなして、はじめて画面表示 $cnt = 1; } # CGIで出力されたデータをHTMLとして認識させるヘッダの出力 print "Content-type: text/html\n\n"; print '
' . "\n"; if ($cnt > 1) { print '再訪問ありがとうございます
' . "\n"; } else { print 'はじめての訪問ですね、クッキーの練習です
' . "\n"; } print '
' . "\n"; print '変数の確認表示' . "\n";
print '
';
print 'cookie: '.$ENV{'HTTP_COOKIE'}.''."\n";
print '
';
print 'cnt :'.$cnt.''."\n";
print '
';
$timenow = &expires(0,0,0,0);
print 'what time now? :'.$timenow.''."\n";
print '
';
print '' . "\n";
}
##########################################
# #
# 入力されたデータのチェックルーチン #
# #
##########################################
sub check {
# クッキー登録でなにも入力がない場合
if ($in{'delete'} ne "on" && $in{'cookie'} eq "on" ) {
if ($in{'name'} eq "" && $in{'email'} eq "" && $in{'biko'} eq "") { &error(2); }}
# メールアドレスが入力されたとき1つの@とその後ろに.が含まれているばOKでそうでないならエラー
if ($in{'email'} ne "") { if (!($in{'email'} =~ /(.*)\@(.*)\.(.*)/)) { &error(0); }}
# メールアドレスが短すぎる場合
if ($in{'email'} ne "") {if (length($in{'email'}) < 6) { &error(3); }}
# クッキーが未登録で、クッキー消去の指定がされたとき
if ($in{'cnt'} == 1 && $in{'delete'} eq 'on') { &error(1); }
# チェック通過
if ($in{'cookie'} eq "on" && $in{'delete'} ne "on") {
# クッキーデータの作成
&setcookie;
}
elsif ($in{'delete'} eq "on") {
# 保存しない場合は、空のデータを保存させることで前のデータを消去する
&delete;
}
# 記録処理後、再読み込みする
print "Location: $reload" . '?' . "\n\n";
exit;
}
#############################################
# #
# チェックエラー表示画面ルーチン #
# #
#############################################
sub error {
# &error(xx); で呼び出されたルーチンは、()内の数字が $error に代入される。
$error = $_[0];
if ($error eq "0") { $error_msg = 'メールアドレスが正しく入力されていません。'; }
elsif ($error eq "1") { $error_msg = 'クッキーがない状態で消去指定をしました。'; }
elsif ($error eq "2") { $error_msg = '入力データがひとつもありません。'; }
elsif ($error eq "3") { $error_msg = 'メールアドレスが短すぎます。'; }
print "Content-type: text/html\n\n";
print '
' . "\n";
print '
';
print 'action: '.$in{'action'}.''."\n";
print '
';
print 'cookie: '.$in{'cookie'}.''."\n";
print '
';
print 'delete: '.$in{'delete'}.''."\n";
print '
';
print 'name: '.$in{'name'}.''."\n";
print '
';
print 'email: '.$in{'email'}.''."\n";
print '
';
print 'biko: '.$in{'biko'}.''."\n";
print '
';
print '
' . "\n"; exit; } ############################################# # # # Set-Cookieヘッダを出力するサブルーチン # # # ############################################# sub setcookie { # クッキーフォーマットは、任意の名称=データの形、よってデータの中には=は使えないので # 項目:データ,項目:データの形で連結した。(簡易BBSより) $cookiedata = "name\:$in{'name'}\,email\:$in{'email'}\,biko\:$in{'biko'}\,cnt\:$in{'cnt'}"; $nisu = $in{'nisu'}; $ji = $in{'ji'}; $fun = $in{'fun'}; $byo = $in{'byo'}; $cookietime = &expires($nisu,$ji,$fun,$byo); print "Set-Cookie: $cookie_name=$cookiedata; expires=$cookietime\n"; # print "$cookie_name=$cookiedata; expires=$cookietime\n"; # print "expires=$cookietime\n"; # print "path=\; "; # print "domain=\n"; print "Content-type: text/html\n\n"; print '
' . "\n";
print 'cookie : '.$cookiedata.''."\n";
print '
';
print 'expires: '.$cookietime.''."\n";
print '
';
print '
' . "\n"; exit; } ############################################# # # # クッキーの消去処理ルーチン # # # ############################################# # expiresになにもセットしなければ、ブラウザ終了時にクッキー自動消去、即消去は現時刻を入れてセット sub delete { $cookietime = &expires(0,0,0,0); print "Set-Cookie: $cookie_name=; expires=$cookietime\n"; # print "$cookie=; "; # print "expires=$cookietime\n"; # print "path=\; "; # print "domain=\n"; print "Content-type: text/html\n\n"; print '
' . "\n";
print 'cookie : '.$cookiedata.''."\n";
print '
';
print 'expires: '.$cookietime.''."\n";
print '
';
print '
' . "\n"; exit; } ######################## # サブルーチン群 # ######################## # 環境変数HTTP_COOKIEを処理するサブルーチン # クッキーを分解して個々の変数に入れる # クッキーの取得(簡易BBS方式) sub cookieCutter { $cookies = $ENV{'HTTP_COOKIE'}; @pairs = split(/;/,$cookies); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $name =~ s/ //g; $DUMMY{$name} = $value; } @pairs = split(/,/,$DUMMY{$cookie_name}); foreach $pair (@pairs) { ($name, $value) = split(/:/, $pair); $COOKIE{$name} = $value; } # 訪問回数に1を加算する $cnt = $COOKIE{'cnt'}; $cnt++; } # Set-Cookie ヘッダの expires の値を計算するサブルーチン # &expires(日,時,分,秒); で 指定時間後の日付が Wdy, DD-Mon-YYYY HH:MM:SS GMT の # 形で返ります。 sub expires { local($day,$ji,$fun,$byo) = @_; local(@date); local(@week) = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); local(@month) = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); # 日時データを算出し、配列に格納 @date = gmtime(time + $day * 60 * 60 * 24 + $ji * 60 * 60 + $fun * 60 + $byo); $date[5] = $date[5] + 1900; foreach (0..3) { $date[$_] = sprintf("%.2d",$date[$_]); } "$week[$date[6]], $date[3]-$month[$date[4]]-$date[5] $date[2]:$date[1]:$date[0] GMT"; } ########################## end_of_script #################################