选择显示字体大小

在线人数统计内幕

     在线人数统计内幕
  
  like most of our topics, i''m covering this one due to user demand for it. if you''ve been to any one of a few dozen asp-based sites recently, you''ve probably seen an example of the script we''ve going to cover in this article. it''s the little line at the top of these pages saying something like "there are 28 users currently on this site!" it''s sort of cute and it gives you an idea of how many people are using the site. i''ve even seen one go so far as to show you a list of where people are on the site. while it''s not something that we''ve chosen to implement, there seems to be enough interest that we thought we should address it. normally i''d cover a topic like this in a sample, but since the bulk of this code needs to go into your global.asa file, i thought i should probably give a more in depth explanation.
  
  what are we counting?
  
  it seems to be a silly question. after all, we just said we were going to count active users. well what exactly is an active user? a connection using http (the protocol of the web) isn''t like other connections. ftp, te.net, e-mail, and most other types of connections are started when a user first visits and ended when the user leaves. http doesn''t work this way. the connection is made, the request for the page is sent, and the connection is dropped. this makes getting the number of people currently "on" a site rather difficult. you don''t have a connection to everyone so you can''t simply count them. you also have no way of telling if they''re going to come back and request another page or not until they do or until you decide you''ve waited long enough that they aren''t going to!
  
  here comes asp with a solution!
  
  the session object. all the session object does is provide us with a temporary storage area on the server for each user. it keeps track of which user belongs to which storage area by using cookies. these cookies never have their expiration property set and as a result, expire as soon as you close your browser. unfortunately, because the session object relies on cookies, if a visitor''s browser doesn''t support them or has them turned off, each request is looked upon as a new session and can cause problems for a site which uses a lot of session objects or does a lot of processing in session_onstart. it also causes a script like this to count every hit as a session! this is the main reason we don''t like this script too much and haven''t used it. the good news is, that for the most part, the fear associated with cookies has been somewhat lessened through user education and the number of users who go out of their way to turn cookies off has decreased significantly.
  
  asa, i thought they were asps!
  
  this brings our discussion to a very special file in your web project: global.asa. global.asa is the file that defines a web application. it contains special scripts called event handlers. there are four such scripts possible with the current version of asp. these are application_onstart, session_onstart, session_onend, and application_onend. the application_onstart script is called when you first start the web application by requesting the first .asp file from it. the application_onend occurs when you shut down the web server (but may not be called if the server freezes up or loses power). as you might expect by now, session_onstart occurs when a user requests their first asp page. but when does session_onend run? the onend event runs when the session ends which can occur in a couple different ways. the first one is you can call the abandon method of the session object which will kill the session immediately. the second is you can wait for the session to timeout. this will take however many minutes you''ve set the session.timeout property to. (the default is 20 if no one''s changed it.) naturally, it''s more desirable to call session.abandon and have the session destroyed immediately and free up the resources it uses instead of letting it use them for 20 minutes.
  
  now to our user count!
  
  like i mentioned earlier most of the code for this particular script needs to go into your global.asa file. for reference, i''m including a global.asa, which includes all the pieces needed to get this script working, below:
  
  global.asa:
  <script language="vbscript" runat="server">
  
  sub application_onstart
   '' set our user count to 0 when we start the server
   application("activeusers") = 0
  end sub
  
  sub session_onstart
   '' change session timeout to 20 minutes (if you need to)
   session.timeout = 20
   '' set a session start time
   '' this is only important to assure we start a session
   session("start") = now
   '' increase the active visitors count when we start the session
   application.lock
   application("activeusers") = application("activeusers") + 1
   application.unlock
  end sub
  
  sub session_onend
   '' decrease the active visitors count when the session ends.
   application.lock
   application("activeusers") = application("activeusers") - 1
   application.unlock
  end sub
  
  </script>
  
  
  
  so what exactly does all that do? well lets take it one section at a time.
  
  application_onstart
  
  the first section of code goes into the application_onstart event handler. like i mentioned earlier, this is run only when the first user hits your web server. what we do in this section is create a variable, which has application scope (which means all sessions can access it and share the same value), named activeusers and set it''s value to 0 since at this point there ar no users on our site. variables of this type are usually just called an application variables.
  
  session_onstart
  
  this is the part of the script which does the real work of keeping track of counting new users. it''s pretty simple, but we do two things you may not be expecting. the first thing we do is set the length of time we want to wait from the time a request is made until we kill the session. this is important, because if you set it to 0 then every request would be considered a session, but on the other hand, the longer you set it for, the more memory and resources get used by your server keeping track of sessions for which the users may have already made their last request. this is too broad a topic to really go into here, but since we don''t have a good reason to change it, we simply use microsoft''s default of 20 minutes. the next thing we do is to ensure that a session is actually started. until there''s a reason to have a session, asp doesn''t always bother tracking them, so it''s possible that even if your browser has full cookie support, you may not actually be assigned to a particular session on your first hit. we avoid this by setting a session variable on initialization so that asp has something to keep track of and actually starts tracking our session.
  
  the remainder of this section is the part that actually increments our user count. first we lock the application object. we need to do this because, like i said before, all our users are sharing it and if multiple people tried to change it at the same time, it could cause unpredictable results. locking it gives the currently running script exclusive access to it and makes any other script which tries to access it wait until we unlock it. we then take the current value of our activeusers variable and increment it by one to account for the user who is just starting their session. finally, we unlock the application object so that others can access it.
  
  session_onend
  
  this code looks remarkable similar to the last few lines in session_onstart. in fact, it''s the exact same code except we decrement the aciveusers variable to account for the user whose session is ending. we lock it before we do this and unlock it afterwards for the same reason as before.
  
  the result
  
  all this work basically leaves us with is one value: the activeusers application variable. it should contain, at all times, the number of open sessions on our web server. this is not necessarily the number of people looking at our page at that exact moment, but it''s about as close an approximation as we can get!
  
  to access this value all you need to do is place a line of code on your page to read the value of this variable. i''ve included a very simple page which does just that below:
  
  somepage.asp:
  <%@ language=vbscript %>
  <html>
  <head>
  <title>active sessions</title>
  </head>
  <body>
  
  <b><font color="#cc0000"><%= application("activeusers") %></font> active users</b>
  
  </body>
  </html>
  
  
  
  
  the only real significant piece of code here is the <%= application("activeusers") %> which reads the value from our variable. the rest is just formatting and can easily be changed so that it matches your site''s colors and/or style.
  
    


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons