本页为历史存档快照(游客可见内容)· 原站已关闭 · 登录/发帖/回复等交互功能不可用 · 标记 "需要登录查看" 的内容当时仅对注册用户开放

哪位E文达人翻译一下,看的晕晕的

发表于 2007-1-9 10:48 · 作者 InTheTerRan · 603 次查看
InTheTerRan 发表于 2007-1-9 10:48
Using the global.asa to schedule ASP code execution.
Have you ever had some asp code that needed to execute every once in a while but, you just didn't know how to do it.

There is a solution that doesn't involve running any scheduling or scripting software on the server and is actually very easy to get working.

You see... there is thing called the "global.asa". Most ASP newbies probably wonder what the heck it even is. The Global.asa file is event driven. It can contain four event procedures: Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd.

The global.asa is basically loaded into memory the first time any user views a page on your Web application. There are event procedure stubs that can contain script you want to be executed when the application starts or ends, or when the session starts or ends.

With some tricky coding you can use this file to schedule code to execute. At least around the time you need it to, this won't be able to make it execute at exactly a certain time.

Here is the 1st example. It simply keeps track of how many visitors have been to your site and after 100 it resets the count back to 0 and executes whatever code you need to run. Obviously you'll need to adjust the "100" to whatever makes sense for the amount of visitors your site receives.

Contents of the global.asa are below.

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

Sub Application_OnStart
Application("SessionCount") = 0
End Sub

Sub Session_OnStart

Application.Lock
Application("SessionCount") = Application("SessionCount") + 1
Application.Unlock

If Application("SessionCount") > 100 Then

Application.Lock
Application("SessionCount") = 0
Application.Unlock

' Here you would put any code you need to run
' do not surround the code with <% %> tags
' For example you might run a database query that checks for expired accounts

End if

End Sub

</SCRIPT>

Now lets say you want something to execute 4 times a day. You can store the date & time in a text file and check it periodically. When the date and time get to be more than 6 hours old the code will write the new date & time to the text file and then execute the code you want to run. You could change the "6" to whatever you want and therefore execute the code more or less often,

This is a pretty slick solution though it requires correct permissions to the text file for reading & writing. If not you'll get an error.

In this example we are checking the text file every 15 visitors. You can adjust this amount or remove the "check" completely so that it checks the file every time, but why check the file every time when you have a very busy site ? That would just be a waste of server resources ,but it is up to you how far you want to take this.

In this example you need to start the text file off with a valid date& time or else you will get an error because the script will read in an empty value the 1st time.

EXAMPLE: put 6/30/99 6:58:45 PM in the 1st line of the text file.

You could add code to check for that and handle the error, but I didn't really care at the time so I didn't do that. As long as there is a date there when it starts it will keep working.

Contents of the global.asa are below.

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

Sub Application_OnStart
Application("SessionCount") = 0
End Sub

Sub Session_OnStart

Application.Lock
Application("SessionCount") = Application("SessionCount") + 1
Application.Unlock

If Application("SessionCount") > 15 Then

Application.Lock
Application("SessionCount") = 0
Application.Unlock

Set ObjMyFile = CreateObject("Scripting.FileSystemObject")
Set OpenMyFile = ObjMyFile.OpenTextFile(Server.MapPath("last-update.txt"))
MyFileValue = OpenMyFile.ReadLine
OpenMyFile.Close

If DateDiff("h",MyFileValue,NOW) > 6 Then

' Here you would put any code you need to run
' do not surround the code with <% %> tags
' For example you might run a database query that checks for expired accounts

Set WriteMyFile = ObjMyFile.CreateTextFile(Server.MapPath("last-update.txt"))
WriteMyFile.WriteLine(NOW)
WriteMyFile.Close

End if
End If

End Sub

</SCRIPT>


Please Note: There are many ways to make this better and many different possible variations of what to check for before executing the desired code. This article should get you started and give you some ideas.

Also remember that if the web is not set up to run as an application the "global.asa" will not run. You'll need to make sure the web is an application. Most Virtual Domains are by default, but sub webs usually are not.

For the sub webs to run the 'global.asa" they need to be an application as the root usually is. In NT this is accomplished via the Internet Service Manager under the properties of the sub web you want to make an application.

Here is what it looks like in IIS4.


One Last Thing: Before you put any code in your Global.asa to execute during events test it first in a regular ".asp" page. If it doesn't run there it sure isn't going to run in your global.asa. Also make sure you file paths are correct for the text file. Everything has to be perfect for this sort of thing to work.

Good Luck
鲨鱼 发表于 2007-1-9 10:49
= =|||代码?
InTheTerRan 发表于 2007-1-9 10:50
你就别管什么了。。翻译过来就行了。。
达伽马 发表于 2007-1-9 10:54
使用 global.asa 预定毒蛇密码实行。
你曾经有需要的一些毒蛇密码三不五时运行但是,你就是没有知道该如何做它。

有不包括跑任何的计划安排或手写体在伺候器上的软件而且是实际上很容易工作的解决。

你见到。。。 有被称为 " global.asa" 的事物。 大多数的毒蛇 newbies 或许怀疑什么木栅它甚至是。 Global.asa 文件是事件受到驱策的。 资讯科技能包含四个事件程序: Application_OnStart , Application_OnEnd , Session_OnStart 和 Session_OnEnd。

global.asa 基本上被载入记忆第一计时任何的使用者看在你的网络申请上的页。当申请开始或结束的时候,有事件程序能包含你想要被运行的手写体的断株, 或当会议开始或结束。

藉由一些狡猾的编码你能使用这一个文件预定编码运行。 至少在当你需要它的时候周围到,这将会无法在完全特定的时间使它运行。

第一个例子在这里。 资讯科技只是掌握的资讯访客曾经到过你的位置多少,而且在 100 之后它把计数重新设定回 0 而且运行你需要跑的任何密码。 明显地你将会需要调整 "100" 到不论什么为你的位置接受的访客数量有道理。

global.asa 的内容在下面。

<手写体语言= VBScript RUNAT=伺候器>

代替 Application_OnStart
申请 (" SessionCount")=0
结束替代人员

代替 Session_OnStart

申请。锁
申请 (" SessionCount")= 申请 (" SessionCount")+1
申请。开启

如果申请 (" SessionCount")>100 然后

申请。锁
申请 (" SessionCount")=0
申请。开启

' 这里你会放你需要跑的任何密码
' 不要包围密码 <%%> 标签
' 举例来说你可能跑一个数据库疑问检查对于期满的帐户

结束如果

结束替代人员

</手写体>

现在让你想要某事一天 4 次运行的发言权。 你能定期地储存一个文字档的日期和时间而且检查它。 当日期和时间开始的时候很旧密码将会写新的日期 & 时间给文字档然后运行你想要跑的密码。 你可以改变 "6" 到无论你想要因此或多或少时常运行密码什么,

虽然它需要为阅读和写作正确的许可对文字档,这是相当光滑的解决。 如果不你将会拿一个错误。

在这一个例子中我们正在检查文字档每个 15个访客。 你能完全地调整这数量或除去 " 检查 " ,以便它每一次检查文件,但是当你有一个非常忙碌的位置时候为什么每一次检查文件 ? 那会仅仅是一个伺候器资源的废物 , 但是依你的意思你想要轮流这多远。

在这一个例子中你需要走开以一个有效的日期和时间开始文字档,否则否则因为手写体将会在空的价值第一次中读,你将会拿一个错误。

例子: 把 6/30/99 下午 6:58:45 放入文字档的第一条线。

你可以增加密码对于那检查而且处理错误,但是我不真的在那时关心,如此我没有做那。 只要在那里有一个日期当它开始的时候 , 它将会保存工作。

global.asa 的内容在下面。

<手写体语言= VBScript RUNAT=伺候器>

代替 Application_OnStart
申请 (" SessionCount")=0
结束替代人员

代替 Session_OnStart

申请。锁
申请 (" SessionCount")= 申请 (" SessionCount")+1
申请。开启

如果申请 (" SessionCount")>15 然后

申请。锁
申请 (" SessionCount")=0
申请。开启

设定 ObjMyFile=CreateObject(" Scripting.FileSystemObject")
固定的 OpenMyFile=ObjMyFile.OpenTextFile(Server.MapPath("持续-update.txt"))
MyFileValue=OpenMyFile。ReadLine
OpenMyFile。结束

如果 DateDiff(" h" , MyFileValue,现在)>6 然后

' 这里你会放你需要跑的任何密码
' 不要包围密码 <%%> 标签
' 举例来说你可能跑一个数据库疑问检查对于期满的帐户

固定的 WriteMyFile=ObjMyFile.CreateTextFile(Server.MapPath("持续-update.txt"))
WriteMyFile.WriteLine(现在)
WriteMyFile。结束

结束如果
结束如果

结束替代人员

</手写体>


请注意: 有许多方法作该检查什么的这个比较好的和许多不同可能变化为在运行被需要的密码之前。 这一个文章应该拿你开始而且给你一些主意。

也记得,如果网没被建立跑如 " global.asa" 将不跑的一个申请。 你将会需要确定,网是一个申请。 大多数的虚拟领域预先设定地 , 但是附属的网通常是不。

对于替代人员设圈套跑那 'global.asa" 他们需要通常是如根的一个申请是。 在新台币中这在附属的网财产之下经由英特网服务经理是完成的你想要制造一个申请。

它所在 IIS4 中看起来像的在这里。


一件最后的事物: 在你之前把任何的密码放入你的 Global.asa 在事件期间运行在一个老客户中测试它第一 ".毒蛇"标明的页数。 如果它在那里不跑它当然不将在你的 global.asa 中跑。 也确定你申请路径对文字档是正确的。 每件事物必须让这种事物是完美的工作。

好运气
鲨鱼 发表于 2007-1-9 10:54
第一句就认识use,the,to,翻译毛阿。。
InTheTerRan 发表于 2007-1-9 10:56
4楼的寒一个,代码都被你翻译成中文了 - -!
鲨鱼 发表于 2007-1-9 10:58
= =|||在线翻译?
达伽马 发表于 2007-1-9 11:00
我没用过在线翻译
鲨鱼 发表于 2007-1-9 11:03
= =|||那。。拜。。。
InTheTerRan 发表于 2007-1-9 11:09
翻译的实在太搞笑了,看来还是要自己慢慢啃。。
radish616 发表于 2007-1-9 14:51
引用:
原帖由 达伽马 于 2007-1-9 10:54 发表
使用 global.asa 预定毒蛇密码实行。
你曾经有需要的一些毒蛇密码三不五时运行但是,你就是没有知道该如何做它。

有不包括跑任何的计划安排或手写体在伺候器上的软件而且是实际上很容易工作的解决。

你 ...
[s:14]
牛  啊  !!!

赞一个!
earthworm 发表于 2007-1-9 16:18
使用global.asa完成asp代码的定时执行
你是否有过一些asp代码需要每隔一段时间就运行一次,但是你不知道如何操作?
这里有一个方法,可以在服务器上不使用任何日程表或脚本软件来简单完成这些操作。
有个东西叫做"global.asa"。许多asp新手可能会以为它是什么黑客软件。Global.asa文件是事件驱动的。它可以保留四个事件过程:Application_OnStart(小程序在运行), Application_OnEnd(小程序结束), Session_OnStart(线程在运行), 还有Session_OnEnd(线程结束)
global.asa在第一次使用web程序浏览页面时仅仅是加载到内存中的。它含有事件过程基点,可以保留你想要在程序或线程开始和结束时执行的脚本。
用些小手段你可以使用这个文件来完成定时执行代码。这不能使程序按照非常精确的时间执行,但是至少可以在你需要的时间里循环。
这里有第一个例子,它仅仅完成记录访问你站点的人数,而且满壹佰后清零,并且同时能够执行你需要运行的任何程序。显然你需要将100的进位放到任何你认为符合逻辑的地方表示。
下面是代码:我就不粘了

最后补充一点:在你将代码放进global.asa文件时,要确保它在普通“.asp”文件上也可以运行,否则在global.asa中也不可能运行。同时确认你的文本文件路径正确。做这种工作每件事都要精益求精。
祝好运。
InTheTerRan 发表于 2007-1-9 16:33
谢楼上的兄弟。。
渴了 发表于 2007-1-9 18:47
貌似和,VB有点关系..................
笨笨s宝宝 发表于 2007-1-9 18:49
这个是不知什么的代码
渴了 发表于 2007-1-9 18:49
四楼的好强,佩服ING
earthworm 发表于 2007-1-9 19:57
引用:
原帖由 笨笨s宝宝 于 2007-1-9 18:49 发表
这个是不知什么的代码
服务器上跑的一段vb脚本

另外希望同志们好好学习英语
因为毕业设计的时候会要求翻译一篇国外的学术论文
有些难度还是挺大的
所以同志们要加油~
笨笨s宝宝 发表于 2007-1-9 20:02
了解~英语。。。。[s:12]