|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
ActiveServerPage技术为应用开发商提供了基于脚本的直观、快速、高效的应用开发手段,极大地提高了开发的效果。在讨论ASP的安全性问题之前,让我们来看看ASP是怎么工作的。准时|实行Usingtheglobal.asatoscheduleASPcodeexecution.
Haveyoueverhadsomeaspcodethatneededtoexecuteeveryonceinawhilebut,youjustdidntknowhowtodoit.
Thereisasolutionthatdoesntinvolverunninganyschedulingorscriptingsoftwareontheserverandisactuallyveryeasytogetworking.
Yousee...thereisthingcalledthe"global.asa".MostASPnewbiesprobablywonderwhattheheckitevenis.TheGlobal.asafileiseventdriven.Itcancontainfoureventprocedures:Application_OnStart,Application_OnEnd,Session_OnStart,andSession_OnEnd.
Theglobal.asaisbasicallyloadedintomemorythefirsttimeanyuserviewsapageonyourWebapplication.Thereareeventprocedurestubsthatcancontainscriptyouwanttobeexecutedwhentheapplicationstartsorends,orwhenthesessionstartsorends.
Withsometrickycodingyoucanusethisfiletoschedulecodetoexecute.Atleastaroundthetimeyouneeditto,thiswontbeabletomakeitexecuteatexactlyacertaintime.
Hereisthe1stexample.Itsimplykeepstrackofhowmanyvisitorshavebeentoyoursiteandafter100itresetsthecountbackto0andexecuteswhatevercodeyouneedtorun.Obviouslyyoullneedtoadjustthe"100"towhatevermakessensefortheamountofvisitorsyoursitereceives.
Contentsoftheglobal.asaarebelow.
<SCRIPTLANGUAGE=VBScriptRUNAT=Server>
SubApplication_OnStart
Application("SessionCount")=0
EndSub
SubSession_OnStart
Application.Lock
Application("SessionCount")=Application("SessionCount")+1
Application.Unlock
IfApplication("SessionCount")>100Then
Application.Lock
Application("SessionCount")=0
Application.Unlock
Hereyouwouldputanycodeyouneedtorun
donotsurroundthecodewith<%%>tags
Forexampleyoumightrunadatabasequerythatchecksforexpiredaccounts
Endif
EndSub
</SCRIPT>
Nowletssayyouwantsomethingtoexecute4timesaday.Youcanstorethedate&timeinatextfileandcheckitperiodically.Whenthedateandtimegettobemorethan6hoursoldthecodewillwritethenewdate&timetothetextfileandthenexecutethecodeyouwanttorun.Youcouldchangethe"6"towhateveryouwantandthereforeexecutethecodemoreorlessoften,
Thisisaprettyslicksolutionthoughitrequirescorrectpermissionstothetextfileforreading&writing.Ifnotyoullgetanerror.
Inthisexamplewearecheckingthetextfileevery15visitors.Youcanadjustthisamountorremovethe"check"completelysothatitchecksthefileeverytime,butwhycheckthefileeverytimewhenyouhaveaverybusysite?Thatwouldjustbeawasteofserverresources,butitisuptoyouhowfaryouwanttotakethis.
Inthisexampleyouneedtostartthetextfileoffwithavaliddate&timeorelseyouwillgetanerrorbecausethescriptwillreadinanemptyvaluethe1sttime.
EXAMPLE:put6/30/996:58:45PMinthe1stlineofthetextfile.
Youcouldaddcodetocheckforthatandhandletheerror,butIdidntreallycareatthetimesoIdidntdothat.Aslongasthereisadatetherewhenitstartsitwillkeepworking.
Contentsoftheglobal.asaarebelow.
<SCRIPTLANGUAGE=VBScriptRUNAT=Server>
SubApplication_OnStart
Application("SessionCount")=0
EndSub
SubSession_OnStart
Application.Lock
Application("SessionCount")=Application("SessionCount")+1
Application.Unlock
IfApplication("SessionCount")>15Then
Application.Lock
Application("SessionCount")=0
Application.Unlock
SetObjMyFile=CreateObject("Scripting.FileSystemObject")
SetOpenMyFile=ObjMyFile.OpenTextFile(Server.MapPath("last-update.txt"))
MyFileValue=OpenMyFile.ReadLine
OpenMyFile.Close
IfDateDiff("h",MyFileValue,NOW)>6Then
Hereyouwouldputanycodeyouneedtorun
donotsurroundthecodewith<%%>tags
Forexampleyoumightrunadatabasequerythatchecksforexpiredaccounts
SetWriteMyFile=ObjMyFile.CreateTextFile(Server.MapPath("last-update.txt"))
WriteMyFile.WriteLine(NOW)
WriteMyFile.Close
Endif
EndIf
EndSub
</p>ASP最大的缺点在于网络的安全性和可靠性,企业将经营数据放在开放的平台上,最大的担忧就是如何保证这些数据不被其他人破坏。 |
|