php實(shí)現(xiàn)計(jì)數(shù)器方法小結(jié)

字號(hào):


    本文實(shí)例講述了php實(shí)現(xiàn)計(jì)數(shù)器的方法。分享給大家供大家參考。具體如下:
    這里收藏了三款php計(jì)數(shù)器代碼,他們?nèi)齻€(gè)都有一個(gè)同共點(diǎn)就是全部無(wú)需數(shù)據(jù)庫(kù),而是利用了文本文件來(lái)實(shí)現(xiàn)網(wǎng)頁(yè)瀏覽計(jì)數(shù).
    第一款PHP計(jì)數(shù)器代碼如下:
    復(fù)制代碼 代碼如下:<?php
    //計(jì)數(shù)器
    function countx($file="count.dat"){
    if(file_exists($file)){
    $fp=fopen($file,"r");
    $numx=fgets($fp,10);
    fclose($fp);
    $numx++;
    //以上四行代碼可以用一條表達(dá)式代替:$numx=file_get_contents($file)+1;
    }
    else{
    $numx=1;}
    file_put_contents($file,$numx);//當(dāng)文件不存在時(shí),這函數(shù)會(huì)自動(dòng)創(chuàng)建文件,而且會(huì)自動(dòng)把參數(shù)轉(zhuǎn)成字符串寫(xiě)入。
    echo $numx;
    /*整個(gè)函數(shù)體可以用兩條表達(dá)式代替:file_exists($file)?file_put_contents($file,file_get_contents($file)+1):file_put_contents($file,"1");readfile($file);
    */
    }
    //函數(shù)調(diào)用
    countx();
    ?>
    第二款PHP計(jì)數(shù)器,代碼如下:
    復(fù)制代碼 代碼如下:<?php
    $counterfile = "balong.txt";//存儲(chǔ)數(shù)值的文件名幾路徑
    function displaycounter($counterfile) {
    $fp = fopen($counterfile,"rw");
    $num = fgets($fp,5);
    $num += 1;
    print "您是第 "."$num"." 個(gè)看巴瀧計(jì)數(shù)器的家伙";
    exec( "rm -rf $counterfile");
    exec( "echo $num > $counterfile");
    }
    if (!file_exists($counterfile)) {
    exec( "echo 0 > $counterfile");
    }
    displaycounter($counterfile);
    ?>
    第三款PHP計(jì)數(shù)器代碼如下:
    復(fù)制代碼 代碼如下:<?php
    $counterfile = "存儲(chǔ)數(shù)值的文件名幾路徑
    function displaycounter($counterfile) {
    $fp = fopen($counterfile,"rw");
    $num = fgets($fp,5);
    $num += 1;
    print "您是第 "."$num"." 個(gè)看巴瀧計(jì)數(shù)器的家伙";
    exec( "rm -rf $counterfile");
    exec( "echo $num > $counterfile");
    }
    if (!file_exists($counterfile)) {
    exec( "echo 0 > $counterfile");
    }
    displaycounter($counterfile);
    ?>
    希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。