20-3 ?用範?一:網??來訪次數

`NϥΨӤpdҨӻpϥ JavaScript iɮŪPؿŪΡC

ڭ̥ݬݭpƾdҡCڭ̦beXӤp`]gйLpƺdҡAǽdҬOQ Application M Session ܼƨӹFpƪ\AѩoܼƳxsbO餤A]oǭpƸƷ|]A}ӬyCӥp`pƺAONpƸxsb@ӭpɮפAíwAШdҡG

Example]fileAccess/pageHitCounter01.asp^G

bWzdҤAunϥΪ̫UsuszvApƾN|[ 1CdҪlXpUG

lɡ]fileAccess/pageHitCounter01.asp^G]ǦϰUYi^
<%@language=JScript%>
<% title="Hɮ׬Dpƾ" %>
<!--#include file="../head.inc"-->
<hr>

<%  
fso = new ActiveXObject("Scripting.FileSystemObject");
countFile = Server.MapPath("pageHitCounter01.cnt");	// XpɮצbwФڦm
fid = fso.OpenTextFile(countFile, 1);			// }ҰŪɮ
count = fid.ReadLine();					// qɮŪXpƸ
fid.Close();						// ɮ
count++;						// W[pƸ
fid = fso.OpenTextFile(countFile, 2);			// }ɮרä\gJ
fid.WriteLine(count);					// gJɮ
fid.Close();						// ɮ
%>

<center>
zO <font color=green><%=count%></font> X.I
<p>]u<a href="javascript:history.go(0)">sz</a>vHW[pƸơC^
</center>

<hr>
<!--#include file="../foot.inc"-->

ѤWz{XiݥXAڭ̥ͤ@ FileObject AAgѦ OpenTextFile kӶ}ҭpɮרæ^ɮ׫ OutAMAg Out ReadLine M WriteLine kӹɮ׶iŪMgJCbϥΦpƺeAڭ̥dzƤ@ӭpɮס]bҬO pageHitCounter.cnt^AHKxspƸơCѩpƸƫxsbɮפA]ä|]A}ӳypƸƪyC

ڭ̤]iHNpƥ\g@ӨơA{X۰ʥhMpɮסAoɭԥunɤJ]tƪɮסANiHƭpƥ\CҦpG

Example]fileAccess/pageHitCounter02.asp^G

bWzdҤAunϥΪ̫UsuszvApƾN|[ 1CdҪlXpUG

lɡ]fileAccess/pageHitCounter02.asp^G]ǦϰUYi^
<%@language=JScript%>
<% title="Hɮ׬Dpƾ" %>
<!--#include file="../head.inc"-->
<hr>

<center>
<!--#include file="counter.inc"-->
zO <font color=green><%=pageHitCounter()%></font> X.I
<%counterFile=Request.ServerVariables("URL") + ".cnt";%>
<p>]pƸxsb <a href="<%=counterFile%>"><%=counterFile%></a>C^
<p>]u<a href="javascript:history.go(0)">sz</a>vHW[pƸơC^
</center>

<hr>
<!--#include file="../foot.inc"-->

pƸƪɮ׫hOb

lɡ]fileAccess/pageHitCounter02.asp.cnt^G]ǦϰUYi^
7166

counter.inc lXpUG

lɡ]fileAccess/counter.inc^G]ǦϰUYi^
<script runat=server language=jscript>
function pageHitCounter(){
	fso = new ActiveXObject("Scripting.FileSystemObject");
	counterFile = Request.ServerVariables("PATH_TRANSLATED") + ".cnt";	// XOɮצbwФڦm
	fid = fso.OpenTextFile(counterFile, 1);			// }ҰŪɮ
	count = fid.ReadLine();					// qɮŪXOƸ
	fid.Close();						// ɮ
	count++;						// W[OƸ
	fid = fso.OpenTextFile(counterFile, 2);			// }ɮרä\gJ
	fid.WriteLine(count);					// gJɮ
	fid.Close();						// ɮ
	return(count);
}
</script>

<script runat=server language=vbscript>
Function pageHitCounter
	Set fso = Server.CreateObject("Scripting.FileSystemObject")
	counterFile=Request.ServerVariables("PATH_TRANSLATED") & ".cnt"	' XOɮצbwФڦm
	'Response.Write(counterFile)
	Set Out= fso.OpenTextFile(counterFile, 1, FALSE, FALSE)	' }ҰŪɮ
	count = Out.ReadLine					' qɮŪXOƸ
	Out.Close						' ɮ
	count= count+1						' W[OƸ
	Set Out= fso.CreateTextFile (counterFile, TRUE, FALSE)	' }ɮרä\gJ
	Out.WriteLine(count)					' gJɮ
	Out.Close						' ɮ
	pageHitCounter=count					' ^Ǹ
End Function
</script>

bWzdҤApɮתWٳOɮצW٦A[W ".cnt"A]unbɤJ count.incAN㦳ӧOpƥ\C~Aɮץ]tӨơAiHOΦb JScript M VBScriptC


JScript {]pPΡGΩAݪ ASP