php實現(xiàn)計數(shù)器方法小結
更新時間:2015年01月05日 09:42:20 投稿:shichen2014
這篇文章主要介紹了php實現(xiàn)計數(shù)器方法,總結了三款非常實用的計數(shù)器實現(xiàn)方法,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了php實現(xiàn)計數(shù)器的方法。分享給大家供大家參考。具體如下:
這里收藏了三款php計數(shù)器代碼,他們三個都有一個同共點就是全部無需數(shù)據(jù)庫,而是利用了文本文件來實現(xiàn)網(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);//當文件不存在時,這函數(shù)會自動創(chuàng)建文件,而且會自動把參數(shù)轉成字符串寫入。
echo $numx;
/*整個函數(shù)體可以用兩條表達式代替:file_exists($file)?file_put_contents($file,file_get_contents($file)+1):file_put_contents($file,"1");readfile($file);
*/
}
//函數(shù)調用
countx();
?>
//計數(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);//當文件不存在時,這函數(shù)會自動創(chuàng)建文件,而且會自動把參數(shù)轉成字符串寫入。
echo $numx;
/*整個函數(shù)體可以用兩條表達式代替:file_exists($file)?file_put_contents($file,file_get_contents($file)+1):file_put_contents($file,"1");readfile($file);
*/
}
//函數(shù)調用
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);
?>
$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 = "www.dhdzp.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");
}
if (!file_exists($counterfile)) {
exec( "echo 0 > $counterfile");
}
displaycounter($counterfile);
?>
$counterfile = "www.dhdzp.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");
}
if (!file_exists($counterfile)) {
exec( "echo 0 > $counterfile");
}
displaycounter($counterfile);
?>
希望本文所述對大家的php程序設計有所幫助。
您可能感興趣的文章:
相關文章
Ubuntu server 11.04安裝memcache及php使用memcache來存儲session的方法
這篇文章主要介紹了Ubuntu server 11.04安裝memcache及php使用memcache來存儲session的方法,涉及memcache服務器的安裝及php操作memcache存儲session的相關技巧,需要的朋友可以參考下2016-05-05
PHP編程過程中需要了解的this,self,parent的區(qū)別
this是指向對象實例的一個指針,在實例化的時候來確定指向;self是對類本身的一個引用,一般用來指向類中的靜態(tài)變量;parent是對父類的引用,一般使用parent來調用父類的構造函數(shù)。2009-12-12
php擴展Zend?Framework框架——Validate擴展
這篇文章介紹了php擴展Zend?Framework框架,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2008-01-01

