PHP制作图形计数器的例子

请看如下例子实现图形计数器,在网页中使用请用 <script src="http://文件地址"></script>

  1. <?
  2. //存放计数的文本文件
  3. $count="count.txt";
  4. //计数器的位数,如果不填则默认为6位
  5. $wei="";
  6. //图像的URL路径
  7. $tu="http://127.0.0.1/tcount/images";
  8. ?>
  9. <?
  10. if ($wei=="") { $wei=6; }
  11. if (!file_exists($count)) { $fp=fopen($count,"w+"); fputs($fp,0,100); fclose($fp); }
  12. if ($REMOTE_ADDR!=$ip) {
  13. $fp=fopen($count,"r+");
  14. $cc=fgets($fp,100);
  15. $cc=trim($cc);
  16. if ($cc=="") { $cou=1; }
  17. else { $cou=$cc+1; }
  18. rewind($fp);
  19. fputs($fp,$cou,100);
  20. fclose($fp);
  21. }
  22. setcookie("ip",$REMOTE_ADDR,time()+86400);
  23. $fp=fopen($count,"r+");
  24. $cou=fgets($fp,100);
  25. $cou=trim($cou);
  26. fclose($fp);
  27. $w=strlen($cou);
  28. while($w<$wei) {
  29. $cou="0".$cou;
  30. $w++;
  31. }
  32. $www=strlen($cou);
  33. $i=0;
  34. echo "document.write("<p align=center>"); ";
  35. while($i<$www) {
  36. $ym=substr($cou,$i,1);
  37. echo "document.write("<img src=$tu/$ym.gif>"); ";
  38. $i++;
  39. }
  40. ?>