php實現(xiàn)計數(shù)器方法小結(jié)
更新時間:2015年01月05日 09:42:20 投稿:shichen2014
這篇文章主要介紹了php實現(xiàn)計數(shù)器方法,總結(jié)了三款非常實用的計數(shù)器實現(xiàn)方法,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了php實現(xiàn)計數(shù)器的方法。分享給大家供大家參考。具體如下:
這里收藏了三款php計數(shù)器代碼,他們?nèi)齻€都有一個同共點就是全部無需數(shù)據(jù)庫,而是利用了文本文件來實現(xiàn)網(wǎng)頁瀏覽計數(shù).
第一款PHP計數(shù)器代碼如下:
復(fù)制代碼 代碼如下:
<?php
//計數(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ù)會自動創(chuàng)建文件,而且會自動把參數(shù)轉(zhuǎn)成字符串寫入。
echo $numx;
/*整個函數(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();
?>
//計數(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ù)會自動創(chuàng)建文件,而且會自動把參數(shù)轉(zhuǎn)成字符串寫入。
echo $numx;
/*整個函數(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計數(shù)器,代碼如下:
復(fù)制代碼 代碼如下:
<?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);
?>
$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ù)器代碼如下:
復(fù)制代碼 代碼如下:
<?php
$counterfile = "www.fzitv.net.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);
?>
$counterfile = "www.fzitv.net.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實現(xiàn)網(wǎng)站訪問量計數(shù)器
- PHP計數(shù)器的實現(xiàn)代碼
- php計數(shù)器的設(shè)計與實現(xiàn)
- 用PHP和MYSQL建立計數(shù)器過程詳解
- 一個自定義位數(shù)的php多用戶計數(shù)器代碼
- 壞狼php學(xué)習(xí) 計數(shù)器實例代碼
- 一個用php3編寫的簡單計數(shù)器
- 調(diào)用WordPress函數(shù)統(tǒng)計文章訪問量及PHP原生計數(shù)器的實現(xiàn)
- php的計數(shù)器程序
- PHP簡單實現(xiàn)文本計數(shù)器的方法
- PHP用函數(shù)嵌入網(wǎng)站訪問量計數(shù)器
相關(guān)文章
Ubuntu server 11.04安裝memcache及php使用memcache來存儲session的方法
這篇文章主要介紹了Ubuntu server 11.04安裝memcache及php使用memcache來存儲session的方法,涉及memcache服務(wù)器的安裝及php操作memcache存儲session的相關(guān)技巧,需要的朋友可以參考下2016-05-05
PHP編程過程中需要了解的this,self,parent的區(qū)別
this是指向?qū)ο髮嵗囊粋€指針,在實例化的時候來確定指向;self是對類本身的一個引用,一般用來指向類中的靜態(tài)變量;parent是對父類的引用,一般使用parent來調(diào)用父類的構(gòu)造函數(shù)。2009-12-12
PHP Undefined index報錯的修復(fù)方法
用$_GET["xx"]這種形式取得數(shù)據(jù)時,如果之前不加判斷,$_GET["xx"]不存在時會出現(xiàn)這樣的警告:PHP Notice: undefined index xxx。2011-07-07
php擴展Zend?Framework框架——Validate擴展
這篇文章介紹了php擴展Zend?Framework框架,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2008-01-01

