文章收藏了三款php計數(shù)器代碼,他們?nèi)齻€都有一個同共點就是全部無需數(shù)據(jù)庫,而是利用了文本文件來實例網(wǎng)頁瀏覽計數(shù).
第一款PHP計數(shù)器代碼如下:
- <?php
- //計數(shù)器
- function countx($file="count.dat"){
- if(file_exists($file)){
- $fp=fopen($file,"r");
- $numx=fgets($fp,10);
- fclose($fp);
- $numx++;
- //以上四行代碼可以用一條表達式代替:$numx=file_get_contents($file)+1;
- }
- else{
- $numx=1;}
- file_put_contents($file,$numx);//當(dāng)文件不存在時,這函數(shù)會自動創(chuàng)建文件,而且會自動把參數(shù)轉(zhuǎn)成字符串寫入。
- echo $numx;
- /*整個函數(shù)體可以用兩條表達式代替:file_exists($file)?file_put_contents($file,file_get_contents($file)+1):file_put_contents($file,"1");readfile($file);
- */
- }
- //函數(shù)調(diào)用
- countx();
- ?>
第二款PHP計數(shù)器,代碼如下:
- <?php
- $counterfile = "balong.txt";//存儲數(shù)值的文件名幾路徑
- function displaycounter($counterfile) {
- $fp = fopen($counterfile,"rw");
- $num = fgets($fp,5);
- $num += 1;
- print "您是第 "."$num"." 個看巴瀧計數(shù)器的家伙";
- exec( "rm -rf $counterfile");
- exec( "echo $num > $counterfile");
- }
- if (!file_exists($counterfile)) {
- exec( "echo 0 > $counterfile");
- }
- displaycounter($counterfile);
- ?>
第三款PHP計數(shù)器代碼如下:
- <?php
- $counterfile = "m.survivalescaperooms.com.txt";//存儲數(shù)值的文件名幾路徑
- function displaycounter($counterfile) {
- $fp = fopen($counterfile,"rw");
- $num = fgets($fp,5);
- $num += 1;
- print "您是第 "."$num"." 個看巴瀧計數(shù)器的家伙";
- exec( "rm -rf $counterfile");
- exec( "echo $num > $counterfile");
- } //開源代碼Vevb.com
- if (!file_exists($counterfile)) {
- exec( "echo 0 > $counterfile");
- }
- displaycounter($counterfile);
- ?>
新聞熱點
疑難解答