毗卢遮那佛咒:如何在自己静态网页上设置计数器,最简单的那种就可以

来源:百度文库 编辑:高考问答 时间:2024/04/28 19:34:29
如何在自己静态网页上设置计数器,最简单的那种,不想用其他网站的代码,想自己做一个,我的空间是动态空间,支持asp,请高手指教。能提供代码和详细的使用方法最好。万分感谢!
请注意我的网页是静态的。请大家给予详细的使用说明和代码。谢谢。

<%@ Language=JavaScript %>
<%
var theTime=new Date();
if (typeof(Application("GuestOnline"))=="undefined")
Application("GuestOnline")="0";
function CheckGuest()
{
if (typeof(Session("UserName"))=="undefined")
Session("UserName")="Guest";
var strUserName=String(Session("UserName"));
var strGuestOnline=String(Application("GuestOnline"));
var GuestArray=strGuestOnline.split("\n");
var i;
var iGuestNum;
if (typeof(Session("GuestNum"))=="undefined")
{
for (i=0;i<GuestArray.length;i++)
{
if (GuestArray[i]=="0") break;
}
Session("GuestNum")=i;
}

iGuestNum=Session("GuestNum")*1;
if (iGuestNum>10000)iGuestNum=10000;

var strFormatTime=theTime.getTime();
strFormatTime="0000000000000000000"+strFormatTime;
strFormatTime=strFormatTime.substr(strFormatTime.length-16,16);

GuestArray[iGuestNum]=strFormatTime+strUserName;

strGuestOnline=GuestArray.join("\n");
Application("GuestOnline")=strGuestOnline;

return 1;
}

function GetNumber()
{
//Count Guests on line
var strFormatTime=theTime.getTime()-24*60*60*1000;
strFormatTime="0000000000000000000"+strFormatTime;
strFormatTime=strFormatTime.substr(strFormatTime.length-16,16);

var strGuestOnline=String(Application("GuestOnline"));
var GuestArray=strGuestOnline.split("\n");

var iGuestCounter=4788;
var i;
for (i=0;i<GuestArray.length;i++)
{
if (GuestArray[i].substr(0,16)> strFormatTime)
iGuestCounter = iGuestCounter + 51;
else if (GuestArray[i].length>0)
GuestArray[i]="0";
}
strGuestOnline=GuestArray.join("\n");
Application("GuestOnline")=strGuestOnline;
return iGuestCounter;
}

CheckGuest();
//Output

Response.Write ("document.write('"+GetNumber()+"');");
%>

把这些代码写进去
<%
CountFile=Server.MapPath("counter.txt")
''文件aspconter.txt是用来储存数字的文本文件,初始内容一般是0
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
Set Out=FileObject.OpenTextFile(CountFile,1,FALSE,FALSE)
counter=Out.ReadLine
''读取计数器文件中的值
Out.Close
''关闭文件
SET FileObject=Server.CreateObject("Scripting.FileSystemObject")
Set Out=FileObject.CreateTextFile(CountFile,TRUE,FALSE)
Application.lock
''方法Application.lock禁止别的用户更改计数器的值
counter= counter + 1
''计数器的值增加1
Out.WriteLine(counter)
''把新的计数器值写入文件
Application.unlock
''使用方法Application.unlock后,允许别的用户更改计数器的值
Response.Write("您是第")
Response.Write("<font color=red>")
Response.Write(counter)
''把计数器的值传送到浏览器,以红(red)色显示给用户
Response.Write("</font>")
Response.Write("位访问者")
Out.Close
''关闭文件
%>
<script>
<!-- Beginning of JavaScript Applet -------------------
function scrollit_r2l(seed)
{ var m1 = "您好,欢迎访问此网站 " ;
var m2 = " " ;
var msg=m1+m2;
var out = " ";
var c = 1;
var speed = 100
if (seed > 100)
{ seed-=2;
var cmd="scrollit_r2l(" + seed + ")";
timerTwo=window.setTimeout(cmd,speed);}
else if (seed <= 100 && seed > 0)
{ for (c=0 ; c < seed ; c++)
{ out+=" ";}
out+=msg; seed-=2;
var cmd="scrollit_r2l(" + seed + ")";
window.status=out;
timerTwo=window.setTimeout(cmd,speed); }
else if (seed <= 0)
{ if (-seed < msg.length)
{
out+=msg.substring(-seed,msg.length);
seed-=2;
var cmd="scrollit_r2l(" + seed + ")";
window.status=out;
timerTwo=window.setTimeout(cmd,speed);}
else { window.status=" ";
timerTwo=window.setTimeout("scrollit_r2l(100)",speed);
}
}
}
scrollit_r2l(100);
// -- End of JavaScript code -->
</script>

最后再在站点里新建一个文本文档名为counter.txt
看看是不是可以了

唉,为什么写这么多代码呢。。。。
本来,就不应该完全给代码的,面对任何人都是。
况且你看看楼主,很明显,他是个比较喜欢钻研的人,计数器自己写,当然是喜欢DIY喽。呵呵。给这么多代码,还有点问题。让人家怎么搞嘛。。。。呵呵。。。开玩笑啦。。。

我说说原理算了。。。
两个办法。
1:JAVASCRIPT做。
写一个函数。读取TXT文件的。里面就是记录你的访问统计信息的。在你的HTML里用这个函数读出你的访问统计出来。就可以了。
2:动态写静态。
把你的HTML的源码写到数据库里,把显示访问的那个数设置为一个特殊字符串,如replace_string1,然后写个ASP,数据库里的HTML源代码读出来。再用replace函数把原来的特殊字符串(这里是replace_string2)换成你的统计数字。再利用ASP的FSO写静态HTML页面。就可以了。呵呵。对了,动态页面里的访问统计你知道写的吧。呵呵。那就ok了。。。

上面两种方法,各有各的好处。。。。
第一种。JS写的,当然,没那么麻烦,而且也很“动态”,缺点,要运行JS代码,每次访问都是同样的速度,“慢”。呵呵。希望你明白引号的意思。。。。(这方法用得比较多一些)
第二种。ASP的FSO写静态页面。应该用的人比较少吧。呵呵。优点,纯HTML,无JS,这样运行速度比较快一点。缺点,如果每次受访问都去重写HTML页面,相对来说就慢了,但是如果你一个星期,一个月更新一次。那么访问速度就比较可观了。

OK。原理都在上面了。写法的话,DIY吧。呵呵。
祝你好运!

以下我段代码最短, 看起来也不费力, 你把它加到网页中就行了 , 但要网页支持JAVASCRIPT
<%
files = Server.MapPath("count.txt");//找文件路径
var fs = new ActiveXObject("Scripting.FileSystemObject");
var f = fs.OpenTextFile(files,1,true,0);//1为打开, true为没有文件时则创建.
var counts = f.ReadLine();
f.Close();

Response.Write(counts);//写入总的访问次数.

counts++;
var fout = fs.OpenTextFile(files,2,true, 0);//0表示以ASSIC格式打开
fout.WriteLine(counts.toString(10)); //10表示10进制
fout.Close();
%>