changeset 2173:44a4dd946a73

クッキー作成機能追加
author SUZUKI Takehisa <c110128@e.koeki-u.ac.jp>
date Mon, 13 Jan 2014 17:13:01 +0900
parents 63ba25d91de9
children 483100968342
files coop/entry.rb
diffstat 1 files changed, 68 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/coop/entry.rb	Wed Dec 18 20:31:20 2013 +0900
+++ b/coop/entry.rb	Mon Jan 13 17:13:01 2014 +0900
@@ -1,36 +1,74 @@
 #!/usr/bin/env ruby
-# coding: euc-jp
+#coding: euc-jp
 
 require 'cgi'
-
-c = CGI.new(:accept_charset => "EUC-JP")
-
-i = 0
-ip = ENV['REMOTE_ADDR']
-
-print "Content-type: text/html; charset=EUC-JP\n\n"
-print "<html>
-<head><title>サンプル</title><link rel=\"stylesheet\" 
-type = \"text/css\" href=\"data.css\"></head><body>"
-
-print"<table>"
-ENV.each { |k,v|
+require 'md5'
 
-    print "<tr><td>#{k}</td><td>#{v}</td></tr>\n"
-}
-
-print"</table>"
-
-srand(56384679263254687)
-10000.times {rand(1000000)}
-idlist = (1..100).collect{rand(10000000)}
-if idlist.index(ARGV[0].to_i)
-  print"<p>おめでとう</p>" 
-  print"<p>#{ip}</p>"
-
-elsif 
-  print"<p>残念</p>"
-  
+#m = MD5.hexdigest('hatsunemiku')
+def generate_session_id
+  m = MD5.new
+  m.update(Time.now.to_s)
+  m.update($$.to_s)
+  m.update(rand.to_s)
+  m.update('secret words')
+  m.hexdigest
 end
 
-print"</body></html>\n"
+cgi = CGI.new('html4Tr')
+me = File.basename(cgi.script_name)
+
+cookie = cgi.cookies['id']
+
+if !cookie.empty?
+  if /^[a-z0-f]{32}$/i =~ cookie.first
+    session_id = cookie.first
+  else
+    session_id = generate_session_id
+#    session_id = m
+  end
+else
+#  session_id = m
+  session_id = generate_session_id
+  cookie = CGI::Cookie.new({'name' => 'id',
+                             'value' => session_id,
+                             'domain' => cgi.server_name,
+                             'path' => cgi.script_name})
+end
+cookie.expires = Time.now + 10
+
+datafile = File.join('/tmp',session_id)
+
+begin
+  if File.exist?(datafile)
+    f = open(datafile,'r+')
+    i = f.read.to_i
+    f.rewind
+  else
+    i = 1
+    f = open(datafile,'w')
+  end
+  f.puts(i+1).to_s
+  f.truncate(f.pos)
+ensure
+  f.close
+end
+
+
+cgi.out({'cookie' => cookie}) do
+  cgi.html({'PRETTY' => ' '}) do
+    cgi.head do
+      cgi.title {'サンプル'}
+    end + cgi.body do
+      srand(56384679263254687)
+      10000.times {rand(1000000)}
+      idlist = (1..100).collect{rand(10000000)}      
+      if idlist.index(ARGV[0].to_i)
+        cgi.p{'おめでとう'} + cgi.p{
+          cgi.code {cookie}
+        }
+      else
+        cgi.p{'ごめんなさい'}
+      end
+    end
+  end
+end

yatex.org