Using ASP to count users ' stay in the site (2)

Source: Internet
Author: User
Tags count log modify sessions valid client
Then you can use the data in any way. You can create an ASP page to read the data and present it to the administrator, or copy it to an electronic worksheet from the database, and then analyze it when you have time.

But keep in mind that using ASP sessions can create problems. In ASP 2.0, sometimes sessions are lost when there is a Global.asa replica in the nested directory under the main application directory. Also, if you use letters in different sizes in URLs, page names, and hyperlinks between pages, browsers like navigator treat URLs as case-sensitive, so you don't send a special ASP session cookie back, so this
The use of the method is also unreliable.

  

"Client Side Cookie" technology
It is also easy to use the client side cookies. The code that completes this work can be placed in an ASP #include file, and then inserted into the Web page where the user will definitely visit. Of course, if you want, you can insert it into all the pages. As long as it works properly during user access, the correct results can be given.

After you set the path and log file name, the code defines a subroutine that attaches a value to the log file as in the previous example of "ASP Sessions." If you want, you can replace the code we use to update a database table instead of a log file.

<%

' Measure visit length with cookie



' Set path and name of log file to is created

' Edit to suit your own machine directory layout

' Remember to give ' directory, Write or full

' CONTROL permission to the IUSR_MACHINE account

strFileName = "C:tempvisit_lengths.txt"



Sub Updatelogfile (intvisitlength)

On Error Resume Next

If intvisitlength > 0 Then

' Got a valid time so enter it into a log file

Strinfo = "session ending in" & Now () _

& "lasted for" & CStr (intvisitlength) & "minute (s)."

' Add user name to the ' log entry string here if required

' Strinfo = strinfo & User Name: & strUserName

Set objfileobject = Server.CreateObject ("Scripting.FileSystemObject")

' Open text file to append data (the ForAppending constant = 8)

Set objfile = Objfileobject.opentextfile (strFileName, 8, True)

objFile.WriteLine Strinfo

Objfile.close

Set objfile = Nothing

Set Objfileobject = Nothing

End If

End Sub



Read a cookie that exists
Now we can do the real work. The rest of the code checks whether an existing cookie is available for this user, and if so, confirms that it contains a valid date and time (we check it must be a date after 1990 years). If the cookie is valid, it then checks if the user has been over 30 minutes after loading the last page (that is, the last time they executed the code). If it's been more than 30 minutes, we'll count it as a new visit, and you can modify that value based on your site and requirements.

...

' Get sessions start time from existing cookies if it exists

Datstart = CDate (Request.Cookies ("Sitevisits") ("StartTime"))

If year (Datstart) > 1990 Then

' Cookie already exists, so get values

Datlast = CDate (Request.Cookies ("Sitevisits") ("Lasttime"))

If (DateDiff ("n", Datlast, Now ()) > Then

' than minutes since last visit so count as new visit

' Get length of ' visit and update log file

Intminutes = DateDiff ("n", Datstart, Datlast)

Updatelogfile intminutes

...

At this point, by executing the Updatelogfile subroutine at the top of the page, we have stored the length of their last visit, which is the number of minutes they visited. You can then update the two values we collected to the current date and time and start recording the length of the visit.

Note that you cannot see any entries in the table until 30 minutes past. In the experiment, you can modify the code with a shorter value.



Length of time to record access
...

' Update values for cookies

' Use new start time and new ' page load ' time

Datstart = Now ()

Datlast = Now ()

Else

...

If the last time we executed this code was less than 30 minutes, we counted it as part of the current visit, so we just need to update the value in the cookie as the time of their last visit:

...

' Less than minutes since last visit so count as the same visit

' Update values for Cookie-just ' "last page load" time

Datlast = Now ()

End If

Else

...



Set default values
The code here is only executed when we don't get a valid cookie from the visitor, so all we can do is use a new cookie for the current date and time to get to the beginning and last value of the last access:

...

' Valid cookie does not exist so set values for a new one

Datstart = Now ()

Datlast = Now ()

End If

...



Create return cookie Value
Now, we've covered all the possible scenarios for the existing values in the cookie, and we've stored the new cookie values in the Datstart and datlast variables. So we can create a cookie to send back to this visitor. Note that each time we recreate the entire cookie, because when we try to modify one of the values and update the cookie, all other existing values are destroyed:

...

' Create cookie to send back to client

' have to recreate whole Cookie-can ' t just change some values

Response.Cookies ("Sitevisits") ("starttime") = Datstart

Response.Cookies ("Sitevisits") ("lasttime") = Datlast

Response.Cookies ("Sitevisits"). Path = "/" ' Apply to entire site



' Make it stay on the user ' s system for three months

Response.Cookies ("Sitevisits"). Expires = DateAdd ("M", 3, now)

% >

One problem with cookie technology is that when a visitor returns to your site, you can only measure the length of his last visit. To do this, we allow cookies to exist on their machines for 3 months, and you can modify this time value to suit your needs.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.