カレンダー
サンプル ⇒ 【カレンダー】
下の欄のソーステキストを、拡張子「.cgi」にペーストして、アップし、ブラウザで見る。
|
#! /usr/local/bin/ruby
today = Time.now
print "Content-type: text/html; charset=Shift_JIS\n\n"
print <<END
<html>
<head>
<title>#{today.year}年カレンダー</title>
</head>
<body bgcolor="mintcream">
<table align="center" cellpadding="4">
<caption style="font-size:large">#{today.year}年</caption>
END
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # 月の日数(うるう年の2月は+1)
days[1] += 1 if today.year % 4 == 0 and today.year % 100 != 0 or today.year
% 400 == 0
for m in 1..12 # カレンダー表示(1月~12月)
startday = Time.local(today.year, m, 1) # 1日
endday = days[m - 1] # 最後の日
print "<tr>\n" if m % 3 == 1
print <<" END"
<td valign="top">
<table border="0" bordercolor="#ffefd5" cellspacing="0"
cellpadding="4">
<tr>
<th colspan="7">#{m}<span style="font-size:xx-small">月</span></th>
</tr>
<tr>
<td align="center" style="font-size:xx-small;color:red">日</td>
<td align="center" style="font-size:xx-small">月</td>
<td align="center" style="font-size:xx-small">火</td>
<td align="center" style="font-size:xx-small">水</td>
<td align="center" style="font-size:xx-small">木</td>
<td align="center" style="font-size:xx-small">金</td>
<td align="center" style="font-size:xx-small;color:seagreen">土</font></td>
</tr>
END
count = 0
i = 0
while i < startday.wday # 1日までの空欄
print "<tr>" if count % 7 == 0
print "<td> </td>"
count += 1
i += 1
end
i = 1
while i <= endday # 日付を書き込む
print "<tr>" if count % 7 == 0
print "<td align=\"right\""
print " style=\"color:red\"" if count % 7 == 0
print " style=\"color:seagreen\"" if count % 7 == 6
print ">"
print i
print "</td>"
count += 1
print "</tr>\n" if count % 7 == 0
i += 1
end
while count % 7 != 0 # 最後の日からの空欄
print "<td> </td>"
count += 1
print "</tr>\n" if count % 7 == 0
end
print <<" END"
</table>
</td>
END
print "</tr>\n" if m % 3 == 0
end
print <<END
</table>
</body>
</html>
END
exit |
訪問回数(クッキー・カウンター)
参考例⇒ 「 【サンプル】 」 色やサイズをアレンジしよう♪
「.cgi」ファイルを作成。
|
#! /usr/local/bin/ruby
require "cgi"
cgi = CGI.new
# Cookieデータ取得
count = cgi.cookies['count'].first
count = count.to_i + 1
# Cookieデータ作成
life = 30 * 24 * 60 * 60
expires = Time.now + life
cookies = [
CGI::Cookie::new({
'name' => 'count',
'value' => count.to_s,
'expires' => expires,
}),
]
cgi.out("cookie" => cookies) do
"
<html>
<head>
<title>訪問回数</title>
</head>
<body>
#{count}回目の訪問<br>
</body>
</html>
"
end
exit |