閲覧者表示2
はじめに
閲覧者表示では初期値が0でしたが、CGIを使って初期値も現在の値にしてみましょう。
サンプルはvisitor.cgiです。
JavaScript
countが0の時は表示フィールドから初期値を取得するように変更
visitor.cgiの呼び出しにreflesh=1を追加
var xmlhttp;
var count = 0;
function check()
{
var o = document.getElementById( "disp" );
if( xmlhttp.readyState == 4 && xmlhttp.status == 200 )
{
c = parseInt( xmlhttp.responseText );
if( count == c )
{
o.style.color = o.parentElement.style.color;
}
else
{
o.innerHTML = c;
if( c < count )
{
o.style.color = 'blue';
}
else
{
o.style.color = 'red';
}
count = c;
}
}
}
function getVisitor()
{
setTimeout( "getVisitor()", 30*1000 );
if( count == 0 )
{
count = parseInt( document.getElementById( "disp" ).innerText );
}
/*@cc_on
@if( @_jscript_version >= 5 )
try
{
xmlhttp = new ActiveXObject( "Msxml2.XMLHTTP" );
}
catch( e )
{
try
{
xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
}
catch( E )
{
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if( !xmlhttp )
{
xmlhttp = new XMLHttpRequest();
}
if( xmlhttp )
{
xmlhttp.onreadystatechange = check;
xmlhttp.open( 'GET', './visitor.cgi?reflesh=1', true );
xmlhttp.send( null );
}
}
var count = 0;
function check()
{
var o = document.getElementById( "disp" );
if( xmlhttp.readyState == 4 && xmlhttp.status == 200 )
{
c = parseInt( xmlhttp.responseText );
if( count == c )
{
o.style.color = o.parentElement.style.color;
}
else
{
o.innerHTML = c;
if( c < count )
{
o.style.color = 'blue';
}
else
{
o.style.color = 'red';
}
count = c;
}
}
}
function getVisitor()
{
setTimeout( "getVisitor()", 30*1000 );
if( count == 0 )
{
count = parseInt( document.getElementById( "disp" ).innerText );
}
/*@cc_on
@if( @_jscript_version >= 5 )
try
{
xmlhttp = new ActiveXObject( "Msxml2.XMLHTTP" );
}
catch( e )
{
try
{
xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
}
catch( E )
{
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if( !xmlhttp )
{
xmlhttp = new XMLHttpRequest();
}
if( xmlhttp )
{
xmlhttp.onreadystatechange = check;
xmlhttp.open( 'GET', './visitor.cgi?reflesh=1', true );
xmlhttp.send( null );
}
}
visitor.cgi
HTMLテンプレートを利用して初期値を出力します。
#!/usr/bin/ruby -Ke
require 'cgi'
require 'htmltemplate'
def visitor( remote_addr )
count = 0
File.open( "visitor.txt", "r+" ) do |file|
file.flock( File::LOCK_EX )
now = Time.now.to_i
exist = false
str = ""
file.each do |line|
ip, time = line.chomp.split( /\t/ )
if ip == remote_addr
time = now
exist = true
end
if time.to_i < now - 40
else
count += 1
str += [ ip, time, "\n" ].join( "\t" )
end
end
if !exist
count += 1
str += [ remote_addr, now, "\n" ].join( "\t" )
end
file.rewind
file.truncate( 0 )
file.write str
end
count
end
cgi = CGI.new()
if cgi["reflesh"].first
print "Content-Type: text/plain\n"
print "Pragma: no-cache\n"
print "Cache-Control: no-cache\n"
print "Expires: Thu, 01 Dec 1994 16:00:00 GMT\n\n"
print visitor( cgi.remote_addr ).to_s
else
html = HTMLTemplate.open( "visitor.html" )
html.gsub!( "(\tinit\t)", visitor( cgi.remote_addr ) )
cgi.out do
html.to_s
end
end
require 'cgi'
require 'htmltemplate'
def visitor( remote_addr )
count = 0
File.open( "visitor.txt", "r+" ) do |file|
file.flock( File::LOCK_EX )
now = Time.now.to_i
exist = false
str = ""
file.each do |line|
ip, time = line.chomp.split( /\t/ )
if ip == remote_addr
time = now
exist = true
end
if time.to_i < now - 40
else
count += 1
str += [ ip, time, "\n" ].join( "\t" )
end
end
if !exist
count += 1
str += [ remote_addr, now, "\n" ].join( "\t" )
end
file.rewind
file.truncate( 0 )
file.write str
end
count
end
cgi = CGI.new()
if cgi["reflesh"].first
print "Content-Type: text/plain\n"
print "Pragma: no-cache\n"
print "Cache-Control: no-cache\n"
print "Expires: Thu, 01 Dec 1994 16:00:00 GMT\n\n"
print visitor( cgi.remote_addr ).to_s
else
html = HTMLTemplate.open( "visitor.html" )
html.gsub!( "(\tinit\t)", visitor( cgi.remote_addr ) )
cgi.out do
html.to_s
end
end
visitor.html
<?xml version="1.0" encoding="euc-jp"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="ja" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>サンプル</title>
<script type="text/javascript" src="./visitor.js"></script>
</head>
<body onload="getVisitor()">
<h1>サンプル</h1>
<p id="disp">
( init )
</p>
<p>
<a href="./">戻る</a>
</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="ja" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>サンプル</title>
<script type="text/javascript" src="./visitor.js"></script>
</head>
<body onload="getVisitor()">
<h1>サンプル</h1>
<p id="disp">
( init )
</p>
<p>
<a href="./">戻る</a>
</p>
</body>
</html>