日韩欧美国产精品免费一二-日韩欧美国产精品亚洲二区-日韩欧美国产精品专区-日韩欧美国产另-日韩欧美国产免费看-日韩欧美国产免费看清风阁

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

利用global.asa定時執行ASP

admin
2012年1月14日 12:46 本文熱度 3440
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


該文章在 2012/1/14 12:46:25 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 色五月日 | 91高清免费国产自产拍 | 亚洲综合国产一区二区三区 | 成人亚欧 | 国产福利在线网址成人 | 欧美乱大交xxxxx变态 | 亚洲国产一区二区试看 | 国产精品欧美精品aⅴ在线 精品欧美在 | 国产又粗又 | 国产欧美日韩一区二区三区在 | 私人尤物在线精品不卡 | 字在线观看一二区 | 在线在线播放 | 亚洲精品国产第一区二区小说 | 日本一二线不卡在线观看 | 亚洲第一天堂m | 中文字幕亚洲一区二区va在线 | 91九色李宗瑞在线观看 | 日韩一二三区视频精品 | 一区二区亚洲日本欧美激情久婷婷 | 亚洲四播房 | 欧美yw精品日本国产精品 | 亚洲第一区欧美国产不卡综合 | 免费影视大全 | 欧美一区二区制服在线 | 国产精品自在拍在线播放大全 | 国产亚洲精品国产福利 | 欧美大片欧美激情性色a∨在线 | 成年免费大片黄在线观看一 | 亚洲无限| 国产精品一区二区三区四区五区 | 国产a在亚洲线播放 | 精品亚洲成a| 最新国产精品拍自在线观看 | 一扒二脱三插片在 | 日本高清中文字幕免费一区二区 | 人与狗性| 亚洲国产一区在线观看 | 日本aⅴ| 成人午夜视频在线观看 | 电影免费在线观看 |