Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [klasa]licznik wejść
Forum PHP.pl > Inne > Oceny
slammer
Zamulałem się na wykładach z flasha więc napisałem sobie klasę liczącą wejścia na stronę. Co o niej sądzicie?

  1. <?php
  2. //wersja pod php 4
  3. class licznik {
  4. var $file;
  5. var $datatab;
  6. var $dzis;
  7.  
  8. function licznik() { //pobierz info
  9. $this->file = 'licz.slm';
  10. $handle = @fopen($this->file, 'a+');
  11. if (!file_exists($this->file)) {
  12.  print 'Brak pliku '.$this->file.'!';
  13. }
  14. else {
  15. $data = @fread($handle, filesize($this->file));
  16. $this->datatab = explode(' ', $data);
  17. }
  18. @fclose($handle);
  19. }
  20.  
  21. function ustaw() {
  22. if ($this->datatab[2] != date("w")){
  23. $this->datatab[1] = $this->datatab[0];
  24. $this->datatab[0] = 0;
  25. }
  26. if ($this->datatab[5] != date("n")) {
  27. $this->datatab[4] = 0;
  28. }
  29. if ($this->datatab[2] == 0 && $this->datatab[7] == 0) {
  30. $this->datatab[6] = 0;
  31. $this->datatab[7] = 1;
  32. }
  33. else {
  34. if ($this->datatab[2] == 0) {
  35. $this->datatab[7] = 1;
  36. }
  37. elseif ($this->datatab[2] != 0) {
  38. $this->datatab[7] = 0;
  39. }
  40.  
  41. }
  42.  
  43. $this->datatab[0]++;
  44. $this->datatab[3]++;
  45. $this->datatab[4]++;
  46. $this->datatab[6]++;
  47.  
  48. $handle = @fopen($this->file, 'w');
  49. $array = array($this->datatab[0], $this->datatab[1], date("w"), $this->datatab[3], $this->datatab[4], date("n"), $this->datatab[6], $this->datatab[7]);
  50. $impl = implode(' ', $array);
  51. @fwrite($handle, $impl);
  52. @fclose($handle);
  53. }
  54.  
  55. function wyswietl() {
  56. $br = nl2br("n");
  57. print 'Dzis: '.$this->datatab[0].$br; //dzis
  58. print 'Wczoraj: '.$this->datatab[1].$br; //wczoraj
  59. print 'Ten tydzien: '.$this->datatab[6].$br; //tydzien
  60. print 'Ten miesiac: '.$this->datatab[4].$br; //miesiac
  61. print 'Wszystkich: '.$this->datatab[3].$br; //calosc
  62. }
  63.  
  64. function cookieProtect() {
  65. setcookie('prtct', md5('1'), time()+3600*24*31);
  66. if ($_COOKIE['prtct'] == md5('1')) {
  67. return false;
  68. }
  69. else {
  70. return true;
  71. }
  72. }
  73. }
  74.  
  75. $objLicznik = new licznik();
  76. if ($objLicznik->cookieProtect()) {
  77. $objLicznik->ustaw();
  78. }
  79. $objLicznik->wyswietl();
  80.  
  81.  
  82. ?>
UDAT
  1. <?php
  2. $handle = @fopen($this->file, 'a+');
  3. if (!file_exists($this->file)) {
  4.  print 'Brak pliku '.$this->file.'!';
  5. }
  6. else {
  7. $data = @fread($handle, filesize($this->file));
  8. $this->datatab = explode(' ', $data);
  9. }
  10. @fclose($handle);
  11. ?>

Czemu najpierw otwierasz plik, potem sprawdzasz czy istnieje?

Gdzie kasujesz odwiedziny poprzedniego dnia?
slammer
Ups faktycznie winksmiley.jpg
nie kasuje odwiedzin poprzedniego dnia tylko zastępuje wynikami z tego dnia:
$this->datatab[1] = $this->datatab[0];
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.