php實現(xiàn)計數(shù)器方法小結
本文實例講述了php實現(xiàn)計數(shù)器的方法。分享給大家供大家參考。具體如下:
這里收藏了三款php計數(shù)器代碼,他們三個都有一個同共點就是全部無需數(shù)據(jù)庫,而是利用了文本文件來實現(xiàn)網頁瀏覽計數(shù).
第一款PHP計數(shù)器代碼如下:
//計數(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ù)器,代碼如下:
$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ù)器代碼如下:
$counterfile = "www.dbjr.com.cn.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程序設計有所幫助。
相關文章
php數(shù)組函數(shù)序列 之array_count_values() 統(tǒng)計數(shù)組中所有值出現(xiàn)的次數(shù)函數(shù)
array_count_values() 函數(shù)用于統(tǒng)計數(shù)組中所有值出現(xiàn)的次數(shù),本函數(shù)返回一個數(shù)組,其元素的鍵名是原數(shù)組的值,鍵值是該值在原數(shù)組中出現(xiàn)的次數(shù)。2011-10-10WordPress中獲取頁面鏈接和標題的相關PHP函數(shù)用法解析
這篇文章主要介紹了WordPress中獲取頁面鏈接和標題的相關PHP函數(shù)用法解析,分別為get_permalink()和wp_title()函數(shù)的使用,需要的朋友可以參考下2015-12-12PHP mail 通過Windows的SMTP發(fā)送郵件失敗的解決方案
今天調試WordPress的郵件發(fā)送功能,總是提示:SMTP server response: 501 5.5.4 Invalid Address。用telnet測試SMTP是沒有任何問題的2009-05-05PHP session有效期session.gc_maxlifetime
PHP中的session有效期默認是1440秒(24分鐘)【weiweiok 注:php5里默認的是180分】,也就是說,客戶端超過24分鐘沒有刷新,當前session就會失效。很明顯,這是不能滿足需要的。2011-04-04php中get_object_vars()在數(shù)組的實例用法
在本篇文章小編給大家整理的是一篇關于php中get_object_vars()在數(shù)組的實例用法,對此有興趣的朋友們可以學習下。2021-02-02