ASP编程:网站统计代码-只需包括此代码便可统计...
只要你想学,就没什么优缺点,上面那位大哥已经把网上的评论说了,但我认为想学哪个都一样,不然它就不可能在当今时代数字艺术方面存活到今天统计Haveyouevertriedtouseoneofthemanyfancy,hightech,multifeatured,superfastandcustommadewebserverstatistics?Idonotknowaboutyou,butIdid.EverytimeIlookedthat20+pagereport,Iwas
prettysurethatsomewherebetweenthousandsoflines,theremustbesimpleanswertomyquestions:How
manytimeswasmypagedownloaded?AfterfewtriesIgaveupandwrotemyown,selfadministrated,
blindinglyobviousserverstatistics.
Ideabehindthiscodeisverysimple,inordertoknowwhatisinterestingtovisitorsofmywebsiteI
haveimplementedthissimplecountertoeachpagewhichwasinterestingtome.Ifyouexamineyoursite,
youcaneasilyfindthattherearetwodifferenttypesof.ASPpages;mostofthemusuallyhaveonlysome
basiccodeforexample;sessionvariablewithbackgroundname,calltolastmodifiedcomponent,orreading
datafromdatabase)whileotherareusedforredirecting,storingdataintodatabases,trigeringmail
componentsetc.Well,mycounterwassuposedtobepresentonallpageswhichhavesomeuserviewable
content.Allyouhavetodoisjusttoincludefilewhichcontainscountercode(wheneverpossibleuse
includefiles,halfofthestrengthofASPliesinpowerincludefiles):
Codesnippetnr.1
1<!--#includevirtual="/marko/last001.asp"-->
Includefileitselfisverysimpleanditcontainsonly30lines.Basically,whatIamdoingisopening
databaseconnection(lines1to4).Inline5IamlookingwhatisexactURLandIamtranslating
everythingintolowercase(remembertoputeverythingintolowercase,becausesomeonecanhaveCAPSLOCK
TURNEDON).Afterthat(lines7to9),IamtryingtofindthisURLinmycounterdatabase.Ifthereisno
recordwiththisexactURL(lines11to16),IamassumingthatthisisnewpageandIaminsertingnew
recordsindatabaseandsettingcounterstoone(well,thisisobviouslyfirsttimethatthispageis
viewed).InthecasethereisrecordwithgivenURL(line18)Iamsimplyincreasingcountervaluesbyone
(lines19to23).Afterthat,takeiteasy,closeyourconnectionsanddestroyusedobjects(lines25to
30).Voila!
Codesnippetnr.2-filecounter.asp
1<%Setcount=Server.CreateObject("ADODB.Connection")
2count.Open"NeT"
3
4setcountrs=Server.CreateObject("ADODB.RecordSet")
5url=LCase(Request.ServerVariables("URL"))
6
7sqlstr="SELECTTOP1*FROMcounterWHEREurl="&url&"ORDERBYurl"
8
9countrs.Opensqlstr,count,3,3,1
10
11ifcountrs.recordcount<1then
12sqlstr="INSERTINTOcounter"
13sqlstr=sqlstr&"(url,total,today,month,last,yesterday)VALUES"
14sqlstr=sqlstr&"("&url&",1,1,1,0,0)"
15
16count.Execute(sqlstr)
17
18ElseIfcountrs.recordcount>0then
19countrs.Fields("today")=CInt(countrs.Fields("today"))+1
20countrs.Fields("month")=CInt(countrs.Fields("month"))+1
21countrs.Fields("total")=CLng(countrs.Fields("total"))+1
22
23countrs.Update
24EndIf
25
26countrs.Close
27count.Close
28
29setcountrs=Nothing
30setcount=Nothing%>
Thereisalsoanotherfilecalledreport.aspwhichwillsimplyrenderdatafromyourdatabaseinone
table.Fileisreallysimpleanditrendersresultsfromyourdatabaseintotable.FirstIamcreatinga
table(lines1to13),openingdatabaseconnectionandselectingallrecords(lines15to17),setting
totalcounterandthismonthcounterto0(lines19and20),andstartingdowhileloopuntilIhitendof
file(line22).Inthetabletherearethreecolumns,onewithURL(seeline26),secondwithtotalnumber
ofhits(line30)andthirdwithtotalnumberofhitsthismonth(line34).NotethatIamalsoincreasing
valuesfortotalcountersforeachcolumn(lines30and34).AfterIexitmyloopIamclosingdatabase
connectionsanddestroyingusedobjects(lines41to45).Ifyoucheckvaluesinvariables"total"
and"month"youwillgettotalnumberpageloadingandtotalnumberofpageloadingincurrentmonth.
Codesnippetnr.3-filereport</p>ActiveServerPage技术为应用开发商提供了基于脚本的直观、快速、高效的应用开发手段,极大地提高了开发的效果。在讨论ASP的安全性问题之前,让我们来看看ASP是怎么工作的。 不能只是将它停留在纸上谈兵的程度上。 完全不知道到底自己学的是什么。最后,除了教程里面说的几个例子,还是什么都不会。 哪些内置对象是可以跳过的,或者哪些属性和方法是用不到的? 兴趣爱好,那么你无须学编程,申请一个域名和空间,在网上下载一些免费开源的CMS系统,你不用改代码,只须熟悉它们的后台操作,像office一样简单方便,很快就能建一个站点,很多站长都是这样做的 没有坚实的理论做基础,那么我们连踏入社会第一步的资本都没有,特别对于计算机专业的学生学好专业知识是置关重要的。在这里我侧重讲一下如何学习ASP,从平时的学习过程中。 代码逻辑混乱,难于管理:由于ASP是脚本语言混合html编程,所以你很难看清代码的逻辑关系,并且随着程序的复杂性增加,使得代码的管理十分困难,甚至超出一个程序员所能达到的管理能力,从而造成出错或这样那样的问题。 先学习用frontpage熟悉html编辑然后学习asp和vbscript建议买书进行系统学习 学习是为了用的,是为了让你的程序产生价值,把握住这个原则会比较轻松点。除此之外,课外时间一定要多参加一些社会实践活动,来锻炼自己的能力。
页:
[1]