VB icon

ezcounter

Email
Submitted on: 1/6/2015 3:14:00 AM
By: Abhi Malani (from psc cd)  
Level: Beginner
User Rating: By 20 Users
Compatibility: ASP (Active Server Pages)
Views: 1632
 
     EzCounter is a Session Counter. In this the Counter increments only once in a Session get the updated article from http://www.duelcom.com/malani/articles/1/
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :ezcounter
'**************************************
Copyright Abhi Malani
abhimalani@yahoo.com
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: ezcounter
' Description:EzCounter is a Session Counter. In this the Counter increments only once in a Session
get the updated article from http://www.duelcom.com/malani/articles/1/
' By: Abhi Malani (from psc cd)
'
' Inputs:<!--#include file="counter.asp"-->
 or
<%Server.Execute("counter.asp")%>
'
' Returns:Returns the no. of counts
To return the no. of counts:
<%=Session("count")%>
or
<%
Response.Write(Session("count"))
%>
'
' Assumes:You need to have read/write permission on the hits.txt file which is in the same directory as this file is.
'**************************************

"COUNTER.ASP" / VBSCRIPT VERSION
<%
If IsEmpty(Session("count")) Then
Call Countthisuser
End if
'by Abhi Malani - http://www.duelcom.com/malani/
Sub CountThisUser()
Dim objFSO, objFS, file, intCount
file = Server.MapPath("hits.txt")
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(file) Then
Set objFS = objFSO.OpenTextFile(file, 1)
Else
Set objFS = objFSO.CreateTextFile(file, True)
End If
If Not objFS.AtEndOfStream Then
intCount = objFS.ReadAll
Else
intCount = 0
End If
objFS.Close
intCount = intCount + 1
Session("count")= intCount
Set objFS = objFSO.OpenTextFile(file, 2)
objFS.Write intCount
objFS.Close
Set objFSO = Nothing
Set objFS = Nothing
End Sub
'Session.Abandon 'for testing the code
%> 
JSCRIPT VERSION/ "COUNTER.ASP"
<%@Language=Jscript%>
<%
//by Abhi Malani - http://www.duelcom.com/malani/
var count = Session("count");
if(count=="" || count==null || count=="undefined"){
countuser();
}
function countuser(){
var ForReading = 1, ForWriting = 2, TristateUseDefault = -2;
var file = "hits.txt";
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFile(Server.Mappath(file));
var ts = f.OpenAsTextStream(ForReading, TristateUseDefault);
var count = ts.ReadAll();
count = parseInt(count);
count = ((isNaN(count)) ? 1 : count++);
ts.Close( );
ts = f.OpenAsTextStream(ForWriting, TristateUseDefault);
ts.Write(count);
ts.Close();
Session("count") = count;
}
//Session.Abandon //for testing the code
%>


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.