|
1 problem with those pix the 0 is on the wrong end so just move it if you are getting 1's instead of 0's.
|
|
|
|
|
Locking the Application before updating the "Counter_?" value is a good idea to avoid having multiple users change the value at the same time, but shouldn't the Application be locked before you load the counter value from the text file? By not locking the application first, you run the risk of two users loading the same counter value and, even though the application is locked before they update the new counter value, they both update the counter with the same value.
I have similar code running in my application's Global.asax Session_Start event because I just need to keep track of unique sessions, but not display that number anywhere in particular. For optimization reasons, using the Session_Start event is a bit better because you don't need to have any "CounterTemp_?" session variables stored in memory. Here is the code that I am using:
Application.Lock()<br />
' Increment hit counter (SessionCounter)<br />
Dim hits As Integer = CInt(Application("SessionCounter")) + 1<br />
Application("SessionCounter") = hits<br />
' Save counter to a text file<br />
Dim fs As FileStream = New FileStream(Server.MapPath("SessionCounter.txt"), FileMode.Create, FileAccess.Write)<br />
Dim sw As StreamWriter = New StreamWriter(fs)<br />
sw.WriteLine(hits)<br />
sw.Close()<br />
fs.Close()<br />
Application.UnLock()<br />
Also, notice I left out some of your conversions to string. Application variables are stored as objects, so theres no need to do a .ToString() on them when storing. Also, there is an overloaded WriteLine() method of the StreamWriter that takes an Integer, so you don't need the Convert.ToString().
Thanks for the great code examples!
|
|
|
|
|
Is there a way to achieve thread safety on the file read operation?
Thanks!
|
|
|
|
|
pshanks wrote: Is there a way to achieve thread safety on the file read operation?
Wow, it's been so long since my post that I don't even remember that code.
Anyway, with my code example, the application is getting locked around the file I/O. As long as that's the only place in code that uses that file, you have already achieved thread safety. If you need to access that file from another place in code, you could create a global mutex instead of doing the application lock, and use that mutex in each place the file is accessed.
|
|
|
|
|
How will this work with load balanced web servers ? It will create text file on server 1, but then next page loads on server 2 and text file isn't present?
Anyway to avoid the Server.MapPath, since you like optimizations.
R.Bischoff | C++
.NET, Kommst du mit?
|
|
|
|
|
Using MSMQ for something like this (at least in concept, not necessarily a counter) might prove useful. The numbers might be slightly off from server to server at any given point (based on network throughput and server CPU usage, etc.), but the lacking any state, a counter is off if a user doesn't regularily and often-enough refresh their page, so no harm done.
For a more serious use, I'm sure you could synchronize the client/server environment.
If nothing else, a "poor man's" approach might be to share the file on a network share, but having to load-balance your site impliess that you have a lot of clients, so this file-based approach wouldn't work for all intents and purposes anyway.
Reminiscent of my younger years...
10 LOAD "SCISSORS"
20 RUN
|
|
|
|
|
This is one of those articles whose code is actually quite simple, but you look at the code and think "Hey, that's really cool!" I had been meaning to see how to return an image dynamically...thanks! The way you did the digits image was cool too. I found this article under "Custom Controls"...this isn't really an asp.net custom control, but it definitely could be. I'd like to make that a suggestion. Then, you just drag/drop it from your toolbox and...voila! Keep up the good work!
|
|
|
|
|
I am working on a dynamic image generating custom control, and so far I can't output imaga data because a custom control uses a Render method which outputs to a textwritter
|
|
|
|
|
|
is the VB...
Otherwise, good example of displaying graphics. I'd use C# since though.
|
|
|
|
|
So you don't like it based on language choice?
Sounds like a resounding Thumbs up to me!
Great snippet of code. I altered it to use a label as the counter (customer didn't want the graphics) and scratched both the read and write to file (as opposed to just the write). Also I put it in a vb file as a function so I can call it anytime within the project, without having to set up an image in teh design view.
Great Work!
|
|
|
|
|
This code is not working for me. Maybe I'm wrong somewhere this is what I do.
I've a webform and in the code I've used it as
<IMG style="Z-INDEX: 107; LEFT: 263px; POSITION: absolute; TOP: 448px" alt="" src="counter.aspx?src=digits.gif&digits=5&id=hk">
I don't have hk.txt (the counterfile) on my disk, so first time it should create it, right. The file is not being created and the counter is not being displayed.
I guess the code for saving to the .txt file is not working ie the filestream code : The following code is not working for me.
dim fs as FileStream = new FileStream(server.mappath(counterid & ".txt"), FileMode.Create, FileAccess.Write)
dim sw as StreamWriter = new StreamWriter(fs)
sw.WriteLine(Convert.ToString(value))
sw.Close
fs.Close
when I debugged I could see that it exits out of the code after the new filestream command.
What could be the reason.
Hari.
|
|
|
|
|
There is a possibility that a file write could fail on your system. Usually the only thing that stops a file write would be permissions.
If the asp.net account is denied for file writes or creation writes then it of course would not be able to create a file. All the permissions that do that stuff run through asp.net's own account (I love that feature btw - no more opening up folders to guest write access, etc )so any other permission restrictions that you may have on your files/directories shouldn't make any difference.
If you are still having a problem, simply turn on your debugging info in the web server. This would give you a full output of the error and probably say that it's permission problems also.
Yeah I was thinking about it a bit more too, and even if you didn't have the digits graphic in your folder, it would still create the counter file. I do highly recommend that you put the counter and it's image(s) in it's own folder in your web structure though to keep it clean. It runs completely independant of any other code, so it doesn't really matter though.
|
|
|
|
|
First off let me congratulate you on a nice piece of code.
I'm new to .NET so if I've missed something obvious please point me at a document and I'll be off.
Now the problem, when view an HTML of ASPX file (that calls your code) inside VS.NET, the count graphic shows up correctly (and count in txt file is incremented). If I attempt to view the same page directly from IE (wwwroot\AppDir\default.html), I get the alternate text and the count is not incremented. This leads me to beleive I have a permissions problem. I have given the ASPNET user full control over the Counter directory but that didn't seem to help.
Any ideas are appreciated.
Thanks,
Shawn
|
|
|
|
|
Of course it will not work if you view it directly like that. Realize that all ASP.NET apps (and all interpreted web languages) need to run through the web server to be executed. ASP.NET can not (and is not supposed to) execute in any browser process.
If you tried to load up an aspx file in the browser for example it would most likely just show text if it would show anything. Your web server (IIS) loads the compiler and executes the code when it encounters an ASP.NET file and then returns that code back to your browser in the form of normal html which the browser can display.
Your browser only natively understands a few basic languages like HTML, VBScript, JavaScript, and XML. The reason you could see it work in the IDE is that the IDE loads the page up in IIS to execute the code and then uses an IE window to display that code.
Try executing the code through your web server like this 'http://localhost/AppDir/default.html' - then you will be viewing the page through your IIS and IIS will compile and run it for you.
|
|
|
|
|
Oops, as soon as I read your reply of course it made sense. I guess I'll have to learn to stop playing with new stuff late at night .
Thanks again,
Shawn
|
|
|
|
|
Hi Everybody,
I just found this hit counter, and I think it's awesome . I did run into a problem running it out of the box. In addition to giving the proper write permissions, you must also make sure that your SessionState in web.config is not set to Off.
Thanks!
|
|
|
|
|
I tried it and it works great. However if I used thre imgOutput.Save(Response.OutputStream, ImageFormat.Jpeg) line, it would output the counter to the page, but it would always put it inot the left hand corner would not show the cotrols I had on the page. Be mindful I have only been doing Web development for about a year and not too experienced.
I changed it to output the image to a file, put an image control on the page and assign the image name as the Image.Imageurl..Works fine on the development side.
When I publish it to the intranet server, the image is blocked, all I see is a box with a read X on it...
What am I missing?? the image is saved ok...
|
|
|
|
|
How do I go about creating my own graphical digit sets?
Bryon Barnard
Barnyard Technologies
barnyardtech@earthlink.net
|
|
|
|
|
You can use any graphic program to make your own digit sets. All you have to do is follow a few rules:
1) all numbers must occupy the same width and height (for example yoo might make all your digits something like 24 X 24 pixels)
2) after making your digits then make one image and put them all in it in order from 0 to 9 (in our example above then this resulting image would be 240(24*10) X 24 pixels)
3) save it as any image format that is compatible with .NET (jpg, gif, bmp, png, etc.)
After creating the image just simply put it in the directory where you have counter.aspx and add it to the command line when you call it
ie.
One of the points of design in this counter script was to make it so it could use any digits. I usually can find ones that I want to use on the web. I'm kinda lazy though and I usually just end up modifing other peep's digits if I need custom ones (usually I just need to change background colors of digits so they look better on my pages)
|
|
|
|
|
Humm . . . I just had a thought. I was thinking about using transparent gifs in the digits. Right now when I output the graphic I'm using jpeg so that all colors in the digits can be displayed.
This works well, but of course jpegs can't have any transparency. I was just thinking that if I changed the code slightly and made it output to PNG then maybe it would keep the transparency from any input gifs and keep all the colors from high color digits.
Anyone know if this would work? And if it does, what would I have to initialize the output graphic as to make it output a PNG correctly (I would think something like 32bpp)?
|
|
|
|
|
Just use the "Argb" formats and structures. There's a lot of documentation about this in the .NET Framework SDK docs and they are quite straight-forward. And yes, it would be 32bpp, unless you used an index image format like GIF (which would be better for most graphical number sets based on file size and transfer rates, not to mention browser support).
Reminiscent of my younger years...
10 LOAD "SCISSORS"
20 RUN
|
|
|
|
|
I make a cool counter prog that no one else has and is simple, fast, efficient, and super easy to use and I get horrible ratings on in . . . bummer
|
|
|
|
|
Look at that again, its been rated 4.29 out of 5, a very good score. The number you were probably looking at was the Popularity, that number takes into account the number of votes (IIRC rating * log<sub>10</sub>(num_votes) ) which is why it was around 3.
James
Sig code stolen from David Wulff
|
|
|
|
|
Yeah it's at >4 now, but it was at like 3 and I was like 'Wow, I suck ass!'
I just kinda figured that if people didn't like it or couldn't use it they would message me . . .
I am more than happy to take suggestions or answer questions if anyone has any probs with it.
|
|
|
|