PHP基於文本的圖形顯示計數器

來源:互聯網
上載者:User

建立 count.txt 文字檔和 index.php,準備 1-9 共九個數位圖片,網上可以找到這類圖片,分別命名為:1-9
count.txt 檔案中,儲存訪問次數,比如輸入100
index.php:

 
  1. <?php 
  2.     //選擇顯示統計資料的顏色 
  3.     //$color_name="black_white"; 
  4.  
  5.     $color_name="white_black"; 
  6.     //$color_name="black_transparent"; 
  7.  
  8.     // 唯讀方式開啟檔案 
  9.     $fp=fopen("counter.txt","r"); 
  10.     //讀取資料 
  11.     $counter=fgets($fp,1024); 
  12.     //關閉檔案 
  13.     fclose($fp); 
  14.     //計數器增加1 
  15.     $counter++; 
  16.     //可寫方式開啟檔案 
  17.     $fp=fopen("counter.txt","w"); 
  18.     //將新的統計資料寫入文字檔 
  19.     fputs($fp,$counter); 
  20.     //關閉檔案 
  21.     fclose($fp); 
  22.      
  23.     //為了防止有其他使用者此時也訪問了該頁面,檔案內容有所改變 
  24.     //重新開啟檔案讀取最新統計資料 
  25.     $fp=fopen("counter.txt","r"); 
  26.     $counter=fgets($fp,1024); 
  27.     fclose($fp); 
  28.  
  29.     //迴圈將統計資料用映像顯示出來 
  30.     //不同的數字針對不同的映像 
  31.     //如數字1,則用映像 1.gif來顯示 
  32.     for ($i=0;$i<strlen($counter);$i++) 
  33.     { 
  34.         $result=$counter[$i]; 
  35.  
  36.         switch($result) 
  37.         { 
  38.             case "0": $ret[$i]="0.gif"; break;    //顯示數字圖片 
  39.             case "1": $ret[$i]="1.gif"; break; 
  40.             case "2": $ret[$i]="2.gif"; break; 
  41.             case "3": $ret[$i]="3.gif"; break; 
  42.             case "4": $ret[$i]="4.gif"; break; 
  43.             case "5": $ret[$i]="5.gif"; break; 
  44.             case "6": $ret[$i]="6.gif"; break; 
  45.             case "7": $ret[$i]="7.gif"; break; 
  46.             case "8": $ret[$i]="8.gif"; break; 
  47.             case "9": $ret[$i]="9.gif"; break; 
  48.         } 
  49.     } 
  50.  
  51.     echo "該頁面的總訪問次數為:"; 
  52.     //迴圈輸出映像 
  53.     for ($i=0;$i<sizeof($ret);$i++) 
  54.         echo "<img border=\"0\" src=\"$color_name/$ret[$i]\" width=\"8\" height=\"11\">"; 
  55. ?> 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.