By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,688 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,688 IT Pros & Developers. It's quick & easy.

Need help with a counter

P: 49
What I have is this php code saved as a .php file
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $count_my_page = ("hitcounter.txt");
  3. $hits = file($count_my_page);
  4. $hits[0] ++;
  5. $fp = fopen($count_my_page , "w");
  6. fputs($fp , "$hits[0]");
  7. fclose($fp);
  8. ?>
And this in my html code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ("counter.php");
  3. ?>
I have never done any webdesign before so I am stumped on how to get this to work. I got the code from some website, what I need is I have a server that does html, no asp or anything, and I need an invisible counter on the page that writes the hit count to a txt file. Any help would be appreciated, THANKS!!
Jun 21 '07 #1
Share this Question
Share on Google+
2 Replies


rnd me
Expert 100+
P: 427
I have a server that does html, no asp or anything,
PHP won't do anything for you in that case.

assuming you can assign write access to a folder or file, you can use the client to do this via AJAX. this is not the most reliable process in the world, but it sure beats nothing.

here is the simplest thing i could think of:
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function IO(U, V) {
  3. X = !!window.XMLHttpRequest ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;
  4. X.open(V ? "PUT" : "GET", U, !!0);
  5. X.send(V ? V : "");
  6. return X.responseText;}
  7. var oD=IO('hit.txt');
  8. IO('hit.txt', oD+'.')
  9. </script>
  10.  
  11.  
the filesize of hit.txt = the number of hits.
Jun 21 '07 #2

pbmods
Expert 5K+
P: 5,821
Heya, tehgreatmg.

If your server has PHP installed, rename your page to have a .php extension and add the include statement at the top of your script.

For example, if your page were called 'home.html', you'd rename it to 'home.php' and add the following code to the top of the page:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ("counter.php");
  3. ?>
  4.  
On the other hand, if (as you mentioned in your OP) you can't use anything other than HTML, you'll have to use an external script to update your hit counter. http://www.google.com/search?q=hit%20counters
Jun 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.