《PHP實(shí)戰(zhàn):php實(shí)現(xiàn)計(jì)數(shù)器方法小結(jié)》要點(diǎn):
本文介紹了PHP實(shí)戰(zhàn):php實(shí)現(xiàn)計(jì)數(shù)器方法小結(jié),希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP應(yīng)用本文實(shí)例講述了php實(shí)現(xiàn)計(jì)數(shù)器的辦法.分享給大家供大家參考.具體如下:
PHP應(yīng)用這里收藏了三款php計(jì)數(shù)器代碼,他們?nèi)齻€(gè)都有一個(gè)同共點(diǎn)就是全部無需數(shù)據(jù)庫,而是利用了文本文件來實(shí)現(xiàn)網(wǎng)頁瀏覽計(jì)數(shù).
PHP應(yīng)用第一款PHP計(jì)數(shù)器代碼如下:
代碼如下:
<?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)成字符串寫入.?
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ù)器,代碼如下:
代碼如下:
<?php?
? $counterfile = "balong.txt";//存儲數(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ù)器代碼如下:
代碼如下:
<?php?
? $counterfile = "www.jb51.net.txt";//存儲數(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應(yīng)用希望本文所述對大家的php程序設(shè)計(jì)有所贊助.
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP實(shí)戰(zhàn):php實(shí)現(xiàn)計(jì)數(shù)器方法小結(jié)》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/12832.html