 |
| Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 13th, 2003, 03:18 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
counter
Hi
I'm using the following code to count how many times users access a page. However, it seems to reset itself every day to 0 again. Can anyone help me with this. Here is the code:
<%Response.Expires = 0%>
<%
If (Application("AppPageCountVB") = "") Then
Application("AppPageCountVB") = 0
End If
Application("AppPageCountVB") = Application("AppPageCountVB") + 1
%>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="right"> visits
since August 1 2003 </div></td>
</tr>
</table>
many thanks
Adam
|
|

August 13th, 2003, 03:48 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Are you restarting the server every day?!
Anyway, you might want to wrap the assignment with .lock and unlock, like this:
Code:
Application.lock
Application("AppPageCountVB") = Application("AppPageCountVB") + 1
Application.unlock
This however won't fix your problem, but it will ensure a (more) precise count.
|
|

August 14th, 2003, 12:27 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
I suggest you use a database to store your stats like site hits, visits, and so on. This is far more reliable than application variables and I agree with Jonax in that if the web service is restarted in any way, it will completely wipe out any existing application variables. There are lots of free hot counter scripts out there which you can download and use.
Cheers!
Marlon Villarama
Support Team
Web Burner Hosting
[email protected]
www.webburner.com
Toll-Free: 877-535-2876
|
|

August 14th, 2003, 05:08 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for your replies. I've managed to find some code that uses the file system object. However it's still not working correctly. It's giving me the following error.
Microsoft VBScript runtime error '800a0035'
File not found
/default.asp, line 112
What's supposed to happen is that a .txt file will be created in a folder that I've created called Counters relating to the page that the code is on. However, it's not working correctly! When I response.write the wfile it gives me the correct path:
D:\Inetpub\WWWroot\st-homeperfect\Counters\defaultasp.txt
The defaultasp.txt is the file that it should be creating.
This is the complete code:
<%
file=request.servervariables("PATH_INFO")
file = replace(file,"/","")
file = replace(file,".","")
if file="" then
file="otros"
end if
Set fs = CreateObject("Scripting.FileSystemObject")
Wfile=server.mappath("\") & "\Counters\" & file & ".txt"
response.Write(Wfile)
'on error resume next
Set a = fs.OpenTextFile(Wfile)
hits = Clng(a.ReadLine)
hits = hits + 1
a.close
if error then
hits = 1
end if
Set a = fs.CreateTextFile(Wfile,True)
a.WriteLine(hits)
a.Close
%>
Number of hits: <% =hits %>
Not sure where to go from here
Cheers
Adam
|
|

August 14th, 2003, 06:52 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You'll need to create the file before you can write to it.
Use the following (FSO) code to determine whether the file exists or not, and create it if it doesn't:
Code:
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(file) = False Then
fso.CreateTextFile file
End If
Set myFile = fso.GetFile(file)
Set ts = myFile.OpenAsTextStream(ForAppending, TristateUseDefault)
ts.Write "Some Text"
Set ts = Nothing
Set myFile = Nothing
Set fso = Nothing
This code uses a TextStream object(ts) which you can get by using the OpenAsTextStream method on a File object.
Check out the FileSystemObject documentation for more information.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

August 14th, 2003, 07:29 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar
Thanks for that - I put that code in but I get an error on the third line where the code reads: fso.CreateTextFile file
The error message is Invalid procedure call or argument
Do I need to set some permissions?
thanks
Adam
|
|

August 14th, 2003, 07:47 AM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello Adam H-W, in order to write to a file on a server you typically have to set the permissions of the specific folder to allow a script to write within that folder. On most servers the CGI folder is automatically setup to allow you to write.
---
David Kittell
Web Developer
TBF Graphics
803 S. Washington Ave.
Saginaw, MI 48601
(989) 752-5540
http://www.tbfgraphics.com
|
|

August 14th, 2003, 08:19 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Adam,
A few additions / remarks to my previous message:
1. Use Server.CreateObject instead of CreateObject
2. Define constants or variables for ForAppending, TristateUseDefault (values of 8 and -2 respectively)
3. If you are using anonymous access to your website, make sure that the IUSR_MachineName account has sufficient rights to the folders where you want to write your files.
4. Make sure, that if you have a product like Norton Anti Virus, the Script Blocking feature is not enabled. Once you have that on, it won't allow you to write files using ASP.
HtH
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

August 14th, 2003, 09:37 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Adam,
The code you sent me has a few flaws. First of all, you try to create a new file, without assigning a filename to the variable file.
Secondly, the rest of the code will probably not work properly too, because you are not checking the existence of the file.
Here's in pseudo code what you should do:
Code:
Declare an instance of the FSO
Use the FSO to see if the file exists (FileExists method)
If TheFileExists Then
Open it and read the number in it
NumOfHits = Read a line from the file
Else
Create a new file
NumOfHits = 0
End If
NumOfHits = NumOfHits + 1
At this point, the file exists and you have the number of hits in NumOfHits, so you can write to the file again, and save NumOfHits to it.
Check the FileSystemObject reference I pointed you to for more details. It has some great examples on this stuff that will make it easy to develop your solution.
HtH
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

August 14th, 2003, 11:24 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks Imar, will try that out
regards
Adam
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Looping with a counter? |
salhabb |
XSLT |
0 |
October 4th, 2005 05:09 AM |
| Hit Counter Help! |
NeilS21 |
Classic ASP Databases |
4 |
April 29th, 2005 06:59 AM |
| counter keeps reseting |
FlashMan |
Classic ASP Professional |
7 |
September 30th, 2004 12:56 PM |
| Counter |
tp194 |
Javascript |
1 |
September 2nd, 2004 08:02 AM |
|
 |