Simple website hit counter (PHP version)

Mushy-pea Mushy-pea is offline Offline Feb 7th, 2007, 8:18 pm |
0
This snippit is a follow up to my first snippit, namely "Simple website hit counter" which is written in Perl. It does exactly the same thing using PHP. "Boring", you might say? Well, I have my reasons for wanting to learn this language and one needs to start somewhere . Also, it will give me a chance to compare how the two languages work for samll programs. If you want to make the counter work on your website you can grab the number tiles from my home page if you like (from the root URL load [0-9]tile.gif). Enjoy.

Steven.
Quick reply to this message  
PHP Syntax
  1. <?php
  2.  
  3. main();
  4.  
  5. function main() {
  6.  
  7. $data = "";
  8. access_file("hit_count.txt", $data);
  9.  
  10. for ($n = 0; $n < strlen($data); $n++) {
  11. $count[$n] = substr($data, $n, 1);
  12. }
  13.  
  14. for ($n = 0; $n < 4 - strlen($data); $n++) {
  15. echo "<img src=\"0tile.gif\" alt=\"0\">";
  16. }
  17.  
  18. for ($n = 0; $n < strlen($data); $n++) {
  19. switch ($count[$n]) {
  20. case 0:
  21. $insert = "0";
  22. break;
  23. case 1:
  24. $insert = "1";
  25. break;
  26. case 2:
  27. $insert = "2";
  28. break;
  29. case 3:
  30. $insert = "3";
  31. break;
  32. case 4:
  33. $insert = "4";
  34. break;
  35. case 5:
  36. $insert = "5";
  37. break;
  38. case 6:
  39. $insert = "6";
  40. break;
  41. case 7:
  42. $insert = "7";
  43. break;
  44. case 8:
  45. $insert = "8";
  46. break;
  47. case 9:
  48. $insert = "9";
  49. break;
  50. default:
  51. $insert = "0";
  52. }
  53. echo "<img src=\"" . $insert . "tile.gif\" alt=\"$insert\">";
  54.  
  55. }
  56.  
  57. }
  58.  
  59. function access_file ($filename, &$data) {
  60. $file1 = fopen($filename, "r+") or die ("Unable to open file $filename for read / write.");
  61. $data = fread($file1, filesize($filename));
  62. $new_data = $data + 1;
  63. fseek($file1, 0, SEEK_SET);
  64. fwrite($file1, $new_data);
  65. fclose($file1);
  66.  
  67. }
  68.  
  69.  
  70. ?>

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC