Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple counter - i think 1

Status
Not open for further replies.

leadman

Programmer
Jun 11, 2001
177
US
Hi all,

I have a single page posted (a survey) and i just want to keep track of two numbers for now. The first is how many hits it gets. The second is how many discreet hits it gets (no double counting the same person coming back again and again). I want these nubers to be available to a second page that will be used simply to display these two numbers. Is this done with application variables?

Chris
 
Hey Chris,

I would use application variables for this. On the page to be counted, I would do something like this:

<cfapplication name=&quot;myApp&quot; sessionmanagement=&quot;yes&quot;>
<cflock name=&quot;myAppVars&quot; timeout=&quot;3&quot;>
<cfif isdefined(&quot;session.visited&quot;)>
<cfset application.uniqueVisitors = application.uniqueVisitors + 1>
<cfset session.visited = &quot;yes&quot;>
</cfif>
<cfset application.count = application.count + 1>
</cflock>

On the view page, I would have this:

<cfapplication name=&quot;myApp&quot; sessionmanagement=&quot;yes&quot;>
<cflock name=&quot;myAppVars&quot; timeout=&quot;3&quot;>
<cfoutput>
Unique Visitors: #application.uniqueVisitors#<p>
Page Hits: #application.count#
</cfoutput>
</cflock>

Hope this helps,
GJ
 
bullseye! thanks -

now how about if i want to track several pages the same way (all reported on a single report page)? Do i repeat the above code on each page but with a different application name?
 
That would do it. Just use something like &quot;application.page1Count&quot;, &quot;application.page2Count&quot;, etc...

GJ
 
uh oh - i didnt do something right?

on the page to be counted i put this:

<cfapplication name=&quot;surveyApp&quot; sessionmanagement=&quot;yes&quot;>
<cflock name=&quot;surveyAppVars&quot; timeout=&quot;3&quot;>
<cfif isdefined(&quot;session.visited&quot;)>
<cfset application.uniqueVisitors = application.uniqueVisitors + 1>
<cfset session.visited = &quot;yes&quot;>
</cfif>
<cfset application.count = application.count + 1>
</cflock>

and got this:

Error resolving parameter APPLICATION.COUNT


The application variable COUNT does not exist. The cause of this error is very likely one of the following things:


The name of the application variable has been misspelled.
The application variable has not yet been created.
The application variable has timed out.

The error occurred while evaluating the expression:


application.count = application.count + 1




The error occurred while processing an element with a general identifier of (CFSET), occupying document position (7:8) to (7:56).

and on the stats page I put this:

<cfapplication name=&quot;surveyApp&quot; sessionmanagement=&quot;yes&quot;>
<cflock name=&quot;surveyAppVars&quot; timeout=&quot;3&quot;>
<cfoutput> Unique Visitors: #application.uniqueVisitors#
<p> Page Hits: #application.count#
<hr>
<p>&nbsp;
</cfoutput>
</cflock>

and got this:

Error resolving parameter APPLICATION.UNIQUEVISITORS


The application variable UNIQUEVISITORS does not exist. The cause of this error is very likely one of the following things:


The name of the application variable has been misspelled.
The application variable has not yet been created.
The application variable has timed out.

The error occurred while evaluating the expression:

#application.uniqueVisitors#


The error occurred while processing an element with a general identifier of (#application.uniqueVisitors#), occupying document position (15:31) to (15:58).

Did i miss the idea? :)
 
Oops, my error.

Add this to the top of each page.

<cflock name=&quot;surveyAppVars&quot; timeout=&quot;3&quot;>
<cfparam name=&quot;application.uniqueVisitors&quot; default=0>
<cfparam name=&quot;application.count&quot; default=0>
</cflock>

GJ
 
0okay - gotta start the application and enable management before working with application variables - got it!
but

The stats page is registering every hit just fine but not registering anything as far as Unique Visitors goes - i called someone and had them go to the page, the hit counted but the unique visitors stayed at zero - no errors though -am i close? heres the relevant code from both pages (forry about the long post):

from the page to be counted:

<cfapplication name=&quot;surveyApp&quot; sessionmanagement=&quot;yes&quot;>
<cflock name=&quot;surveyAppVars&quot; timeout=&quot;3&quot;>
<cfparam name=&quot;application.uniqueVisitors&quot; default=0>
<cfparam name=&quot;application.count&quot; default=0>
</cflock>

<cflock name=&quot;surveyAppVars&quot; timeout=&quot;3&quot;>
<cfif isdefined(&quot;session.visited&quot;)>
<cfset application.uniqueVisitors = application.uniqueVisitors + 1>
<cfset session.visited = &quot;yes&quot;>
</cfif>
<cfset application.count = application.count + 1>
</cflock>

And from the page that shows the counts:

<cfapplication name=&quot;surveyApp&quot; sessionmanagement=&quot;yes&quot;>
<cflock name=&quot;surveyAppVars&quot; timeout=&quot;3&quot;>
<cfparam name=&quot;application.uniqueVisitors&quot; default=0>
<cfparam name=&quot;application.count&quot; default=0>
</cflock>
//....some html body stuff.....//
<cflock name=&quot;surveyAppVars&quot; timeout=&quot;3&quot;>
<cfoutput> Unique Visitors: #application.uniqueVisitors#
<p> Page Hits: #application.count#
<hr>
<p>&nbsp;
</cfoutput>
</cflock>
 
Oops, I really messed this one up.

Change &quot;<cfif isdefined(&quot;session.visited&quot;)>&quot; to &quot;<cfif not isdefined(&quot;session.visited&quot;)>&quot; and I think it will work.

GJ
 
hey that helped quite a bit - i think im starting to understand how this works (been a little shaaky with session and application concepts) - but i hate to end this long thread yet soooo...:)

when i keep refreshing the page that is being counted, the page hits on the stats page keep incrementing - that is good. The Unique Visits number just goes up once - that is good. If i wait a few minutes and refresh the first page again and then check the couters again both numbers go up - that is not good. Why does the Unique Visits counter count me as another unique visitor if i wait a few minutes? something to do with the session timeout maybe?
 
Yes, when your session times out then you will appear to be a new visitor and thus the unique visitor count will be incremented. You can change your session timeout within a parameter of the <cfapplication> tag to extend the session although the CF admin has a configurable limit which you can't exceed.

If the session timeout is going to be a problem, you could always use a client or cookie variable to store the &quot;visited&quot; variable but these require that cookies be enabled on the client's PC. These also would cause two people using the same PC to appear to be one visitor from places such as libraries, cyber cafes, mall kiosks, etc... I think your best bet is to use sessions but set your session timeout high enough to give an accurate count.

GJ
 
great - thank, i changed the session timeout to be more realistic - i used createtimespan - so what does timeout '3' do (for the lock) lock for 3 seconds?
and - my stats page reset itself it seems today - did my application timeout? if so - how can i keep that from happening (since i want to keep a running count of visits indefinitely)
 
sorry about all the posts here but im a little dense when it comes to application and session variables. Im using several resources to try and get it but that doesnt always help. For instance, at cfhub.com, they state the following

&quot;There is something else special about the application.cfm file. It is the only file that allows the use of the CFAPPLICATION tag.&quot;

Didnt we just use the cfapplication tag on the page i wanted counted? (which was not the application.cfm file)

confused
 
hi leadman

As long as you have the CFAPPLICATION tag where it counts, in application.cfm, it doesn't matter how many other locations that tag is found in. CF will simply ignore it. If you're able to access application variables at all, then your application.cfm file is fine.

 
Hey leadman,

I've never heard of that restriction on the <cfapplication> tag although I wouldn't have it in two places at once just for &quot;clean code&quot; reasons. There might have been a restriction like that in an earlier release but I don't think any release from 4 up has it.

Also, the timeout is in seconds. It tells the lock how long to wait before giving up and continuing past or erroring out. The &quot;throwontimeout&quot; setting tells it which of these two actions to take. Erroring out is the default behavior.

Hope this helps,
GJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top