最簡(jiǎn)單的PHP程序--記數(shù)器
更新時(shí)間:2006年10月09日 00:00:00 作者:
原理:
1.第一位使用者瀏覽某頁(yè)。
2.伺服器程式從資料庫(kù)或檔案中讀取該頁(yè)被瀏覽次數(shù)。
3.將次數(shù)加一儲(chǔ)存,并將它送回第一位使用者。
4.第二位使用者瀏覽某頁(yè)。
5.伺服器程式從資料庫(kù)或檔案中讀取該頁(yè)被瀏覽次數(shù)。
6.將次數(shù)再加一儲(chǔ)存,并將它送回第二位使用者。
需要了解的函數(shù):
fopen()打開文件
filesize()獲得文件大小
fseek()移動(dòng)文件指針
fgets()得到文件指針?biāo)谛袃?nèi)容
fputs()將字串寫如文件指針?biāo)谖恢?
fclose()關(guān)閉文件
file_exists()判斷文件是否存在
exec()執(zhí)行外部程序
最簡(jiǎn)單的記數(shù)器:
<html>
<head>
<title>訪客計(jì)數(shù)器 原型</title>
</head>
<body>
<?php
/*
(c)1998 David W. Bettis
這里是版權(quán)信息
*/
$counterFile = "counter.txt";
#這里是定義記數(shù)器文件
function displayCounter($counterFile) {
$fp = fopen($counterFile,"rw");
#打開文件,用讀寫方式
$num = fgets($fp,5);
#取得當(dāng)前數(shù)字
$num += 1;
#加1
print "您是第 "."$num"." 位無(wú)聊份子";
exec( "rm -rf $counterFile");
exec( "echo $num > $counterFile");
#偷懶的方式哦,不使用fputs寫入
}
if (!file_exists($counterFile)) {
exec( "echo 0 > $counterFile");
}#如果記數(shù)器文件不存在,新建它并設(shè)置內(nèi)容為0
displayCounter($counterFile);
?>
</body>
</html>
PHP記數(shù)器比較簡(jiǎn)單版:
<?
#版權(quán)沒有啦,這么簡(jiǎn)單
$fp=fopen("counter.txt","r+");
flock($fp,3);
#打開記數(shù)器文件并鎖住
$fsize=filesize("count.txt");
$count=fgets($fp,$fsize+1);
$count++;
#取得數(shù)碼并加一
fseek($fp,0);
fputs($fp,$count);
fclose($fp);
#將新數(shù)碼寫入文件
echo "你是第 $count 位訪問者";
?>
PHP記數(shù)器圖形版:
制作10個(gè)圖片,將數(shù)字串用圖片組起來(lái),我就不細(xì)說(shuō)了
假設(shè)圖片為0.gif ~ 9.gif
<?
....$count為取得的數(shù)值
$strcount=strval($count);
$strcount=chop($strcount);
$countlen=$strlen($strcount);
$shtml="";
for ($i=0; $i<$countlen; $i++) {
$shtml.="<img src='";
$shtml.=$strcount[$i];
$shtml.=".gif'>";
}
echo $shtml;
?>
PHP記數(shù)器數(shù)據(jù)庫(kù)版:
使用SQL記數(shù)器,先建好表
CREATE TABLE counter
(
counter int not null,
id int not null
)
INSERT INTO counter(counter,id) VALUE(0,1)
<?
$conn=mysql_connect(..., ..., ...);
#MySQL數(shù)據(jù)庫(kù)連接
$sql="select * from counter";
$result=mysql_query($sql,$conn);
$objresult=mysql_fetch_object($result);
$count=$objresult->counter;
$count++;
$sql="update counter set counter=".$count."where id=1";
mysql_query($sql,$conn);
mysql_close($conn);
echo "你是第$count位訪客";
?>
1.第一位使用者瀏覽某頁(yè)。
2.伺服器程式從資料庫(kù)或檔案中讀取該頁(yè)被瀏覽次數(shù)。
3.將次數(shù)加一儲(chǔ)存,并將它送回第一位使用者。
4.第二位使用者瀏覽某頁(yè)。
5.伺服器程式從資料庫(kù)或檔案中讀取該頁(yè)被瀏覽次數(shù)。
6.將次數(shù)再加一儲(chǔ)存,并將它送回第二位使用者。
需要了解的函數(shù):
fopen()打開文件
filesize()獲得文件大小
fseek()移動(dòng)文件指針
fgets()得到文件指針?biāo)谛袃?nèi)容
fputs()將字串寫如文件指針?biāo)谖恢?
fclose()關(guān)閉文件
file_exists()判斷文件是否存在
exec()執(zhí)行外部程序
最簡(jiǎn)單的記數(shù)器:
<html>
<head>
<title>訪客計(jì)數(shù)器 原型</title>
</head>
<body>
<?php
/*
(c)1998 David W. Bettis
這里是版權(quán)信息
*/
$counterFile = "counter.txt";
#這里是定義記數(shù)器文件
function displayCounter($counterFile) {
$fp = fopen($counterFile,"rw");
#打開文件,用讀寫方式
$num = fgets($fp,5);
#取得當(dāng)前數(shù)字
$num += 1;
#加1
print "您是第 "."$num"." 位無(wú)聊份子";
exec( "rm -rf $counterFile");
exec( "echo $num > $counterFile");
#偷懶的方式哦,不使用fputs寫入
}
if (!file_exists($counterFile)) {
exec( "echo 0 > $counterFile");
}#如果記數(shù)器文件不存在,新建它并設(shè)置內(nèi)容為0
displayCounter($counterFile);
?>
</body>
</html>
PHP記數(shù)器比較簡(jiǎn)單版:
<?
#版權(quán)沒有啦,這么簡(jiǎn)單
$fp=fopen("counter.txt","r+");
flock($fp,3);
#打開記數(shù)器文件并鎖住
$fsize=filesize("count.txt");
$count=fgets($fp,$fsize+1);
$count++;
#取得數(shù)碼并加一
fseek($fp,0);
fputs($fp,$count);
fclose($fp);
#將新數(shù)碼寫入文件
echo "你是第 $count 位訪問者";
?>
PHP記數(shù)器圖形版:
制作10個(gè)圖片,將數(shù)字串用圖片組起來(lái),我就不細(xì)說(shuō)了
假設(shè)圖片為0.gif ~ 9.gif
<?
....$count為取得的數(shù)值
$strcount=strval($count);
$strcount=chop($strcount);
$countlen=$strlen($strcount);
$shtml="";
for ($i=0; $i<$countlen; $i++) {
$shtml.="<img src='";
$shtml.=$strcount[$i];
$shtml.=".gif'>";
}
echo $shtml;
?>
PHP記數(shù)器數(shù)據(jù)庫(kù)版:
使用SQL記數(shù)器,先建好表
CREATE TABLE counter
(
counter int not null,
id int not null
)
INSERT INTO counter(counter,id) VALUE(0,1)
<?
$conn=mysql_connect(..., ..., ...);
#MySQL數(shù)據(jù)庫(kù)連接
$sql="select * from counter";
$result=mysql_query($sql,$conn);
$objresult=mysql_fetch_object($result);
$count=$objresult->counter;
$count++;
$sql="update counter set counter=".$count."where id=1";
mysql_query($sql,$conn);
mysql_close($conn);
echo "你是第$count位訪客";
?>
相關(guān)文章
PHP預(yù)定義變量9大超全局?jǐn)?shù)組用法詳解
本文主要介紹PHP9個(gè)超全局?jǐn)?shù)組$_SERVER、$_GET、$_POST、$_COOKIE、$_FILES、$_ENV、$_REQUEST、$_SESSION、$GLOBALS的詳細(xì)用法。2016-04-04用PHP實(shí)現(xiàn)小型站點(diǎn)廣告管理(修正版)
用PHP實(shí)現(xiàn)小型站點(diǎn)廣告管理(修正版)...2006-10-10用libtemplate實(shí)現(xiàn)靜態(tài)網(wǎng)頁(yè)生成
用libtemplate實(shí)現(xiàn)靜態(tài)網(wǎng)頁(yè)生成...2006-10-10無(wú)數(shù)據(jù)庫(kù)的詳細(xì)域名查詢程序PHP版(2)
無(wú)數(shù)據(jù)庫(kù)的詳細(xì)域名查詢程序PHP版(2)...2006-10-10用php寫的serv-u的web申請(qǐng)賬號(hào)的程序
用php寫的serv-u的web申請(qǐng)賬號(hào)的程序...2006-10-10