Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jun 2007
Posts: 5
Reputation: tehgreatmg is an unknown quantity at this point 
Solved Threads: 0
tehgreatmg tehgreatmg is offline Offline
Newbie Poster

Hit counter

 
0
  #1
Jun 21st, 2007
I need help with writing a hit counter in javascript. I am new to weboage design so I really dont know how to go about doing this.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,610
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 465
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Hit counter

 
0
  #2
Jun 21st, 2007
Read this.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Hit counter

 
0
  #3
Jun 21st, 2007
...if you want to write a hitcounter that records hits from any and all users; you cannot use Javascript - it can't write data to anywhere except cookies, which are unique to each user's browser.

so, you'll have to use some serverside application/script/method to record data permanantly at your server.

PHP: http://www.developingwebs.net/phpclass/hitcounter.php

Perl/SHTML:http://www.akamarketing.com/simple-h...with-perl.html

C++: http://www.daniweb.com/code/snippet596.html

ASB (with VBScript): http://www.webwizguide.com/asp/tutor...r_tutorial.asp

etc..

The key is, these are programs that run at the server, Javascript in a browser is a client program. Javscript when used as an ASP scripting language is serverside, but the details of making a hitcounter using that would be more of an ASP question than a Javscript question.

You can use AJAX browser Javascript to talk to server programs, but the server program has to be there to record permanent non user-bound data, and infact, any kind of permanent data that you yourself can read..
Last edited by MattEvans; Jun 21st, 2007 at 1:06 pm.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 5
Reputation: tehgreatmg is an unknown quantity at this point 
Solved Threads: 0
tehgreatmg tehgreatmg is offline Offline
Newbie Poster

Re: Hit counter

 
0
  #4
Jun 21st, 2007
Thanks MattEvans, I was afraid you couldnt do this through javascript, I will give the info you gave me a shot.
Last edited by tehgreatmg; Jun 21st, 2007 at 2:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 5
Reputation: tehgreatmg is an unknown quantity at this point 
Solved Threads: 0
tehgreatmg tehgreatmg is offline Offline
Newbie Poster

Re: Hit counter

 
0
  #5
Jun 21st, 2007
OK I looked at what you posted and just got confused. I understand how those work, the main thing I still can not grasp is how to actually call the script from the html code. Can I write either a c++ or vbscript that can be called from the html code source?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Hit counter

 
0
  #6
Jun 21st, 2007
Well. there are a few methods for getting script output 'into' HTML. But, you have to understand; HTML is not a programming language, it's a document markup language.

One way to get script output into HTML is to generate HTML entirely programmatically. That is, 'print' HTML code directly to a users browser, and the parts that are 'dynamic' can just be printed in with the rest of the HTML. That principle is used quite heavily in languages like PHP; you can write pure HTML, and write blocks of PHP code directly inside the HTML code using 'special markers' to determine the start and end of the PHP. The page is prepared when the user requests it, and sent back different each time. ASP uses the same principle ( embeded code in HTML ). Perl and C++ do the opposite; you output HTML by literally printing it "to the user's browser" as a string.

You can also use something called SHTML ( server-parsed HTML ), it's a very basic system; you write special comments in your HTML code like this: <!--#directive parameter="value"-->, the server looks for those comments, and replaces them by performing the provided directive; an example is: <!--#include virtual="path_to_script"--> : the server will read that when an HTML page is requested; look for the script at "path_to_script" on the server itself, execute it, and place the reply from the script in the data that is sent back to the browser. You need to save the files that do such includes as 'file.shtml' instead of 'file.html', and you need to check whether your server supports it before you rely on it for something..

http://httpd.apache.org/docs/1.3/howto/ssi.html

Both of these methods are very similar, and can actually be used together, i.e. you can write blocks of Perl code that print out 'special' elements ( like a little <div> element containing the number of hits on your page for example =P ) save it somewhere on the server, and then embed the output of that script on every page that's requested using an SHTML include directive. Running the script (by including it) is what increments the counter, so, everytime a page is requested, the counter can be updated.

Both of these methods rely totally on preparation at the server. You have to regenerate the page everytime it's requested. That can affect caching optimizations.. i.e. you CANNOT cache pages that are really dynamic, meaning they have to be downloaded direct from your server everytime they are viewed.

The only method that isn't entirely server-generated is to write a script on the server that does the 'work' and then use a method called AJAX (A)syncronous (J)avascript (A)nd (X)ML to download data from the server and display it within a page when the page has already been downloaded. Erm. I would say, if you want to do that; write the working script on the server and use SHTML to include it first; because it'll teach you the basics, and AJAX is pretty easy if you know Javascript/XML basics already.

http://en.wikipedia.org/wiki/Common_Gateway_Interface
Last edited by MattEvans; Jun 21st, 2007 at 4:35 pm.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 5
Reputation: tehgreatmg is an unknown quantity at this point 
Solved Threads: 0
tehgreatmg tehgreatmg is offline Offline
Newbie Poster

Re: Hit counter

 
0
  #7
Jun 21st, 2007
I am still lost. What I have is this in my html code
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript" src="counter.php" type="text/JavaScript"></script>

and this php script
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2. session_start ();
  3.  
  4. // get current hit
  5. $opFile = fopen ("counter.txt", "r");
  6. $handle = fread ($opFile, filesize ("counter.txt"));
  7. fclose ($opFile);
  8.  
  9. // if new session
  10. if (!isset ($_SESSION['hit'])){
  11.  
  12. // set session
  13. $_SESSION['hit'] = TRUE;
  14.  
  15. // add the hit
  16. $handle = $handle + 1;
  17.  
  18. // print javascript
  19. echo 'document.write("'.$handle.' Hits");';
  20.  
  21. // put new hit to db
  22. $opFile = fopen ("counter.txt", "w");
  23. fwrite ($opFile, $handle);
  24. fclose ($opFile);
  25.  
  26. // else
  27. }else{
  28.  
  29. // print only
  30. echo 'document.write("'.$handle.' Hits");';
  31. }
  32. ?>

and then a file named counter.txt with only the value 0 in it. All three files are in my C:\inetpub\wwwroot folder

And when I open the webpage the number in counter.txt does not change and no counter is displayed on the page.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Hit counter

 
0
  #8
Jun 21st, 2007
First, the files need to be on and accessed via a HTTP server (with PHP installed), not just in a local folder...

If they are; what happens when you just access 'http://yourdomain.tld/counter.php"?

You should also add this line to the php file, put it right at the top after the <?php tag:

  1.  
  2. header("Content-Type: text/javascript");

Some browsers will complain if you ommit that.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 5
Reputation: tehgreatmg is an unknown quantity at this point 
Solved Threads: 0
tehgreatmg tehgreatmg is offline Offline
Newbie Poster

Re: Hit counter

 
0
  #9
Jun 26th, 2007
When I access http://localhost/counter.php I get this:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.write("1 Hits");PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: open(C:\DOCUME~1\c9972442\LOCALS~1\Temp\php\session\sess_ql1eb53bk1s8ohkja7ivd6tb13, O_RDWR) failed: Permission denied (13) in C:\Inetpub\wwwroot\counter.php on line 3
  2. PHP Warning: fopen(counter.txt) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Permission denied in C:\Inetpub\wwwroot\counter.php on line 23
  3. PHP Warning: fwrite(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\counter.php on line 24
  4. PHP Warning: fclose(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\counter.php on line 25
  5. PHP Warning: Unknown: open(C:\DOCUME~1\c9972442\LOCALS~1\Temp\php\session\sess_ql1eb53bk1s8ohkja7ivd6tb13, O_RDWR) failed: Permission denied (13) in Unknown on line 0
  6. PHP Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (C:\DOCUME~1\c9972442\LOCALS~1\Temp\php\session) in Unknown on line 0

It looks to me that it will just not let me open the file due to permissions.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 164
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Hit counter

 
0
  #10
Jun 29th, 2007
Did you save a zero count file to get it started?

You can't open the file for read until it exists.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC