计数器
原创于www.dukejava.com
基于文件的php计数器
〈?
//php计数器,基于文件系统。
function wincounter(){
//如果文件不存在,创建之
if(!file_exists(count.txt))
{
exec(echo 0>count.txt);
}
//打开我们的记录文件
//得到文件大小然后依据这个文件的大小取出需要的数据
$fp=fopen(count.txt,r+);
$filesize=filesize(count.txt);
$count=fgets($fp,$filesize+1);
//将记录数加上1以后存回文件中
$count+=1;
fseek($fp,$count);
fclose($fp);
//返回现在的访问数
return $count;
}
?〉
基于数据库的计数器(mysql)?
1、首先创建数据库:
create table counter{
counter int not null,
id int not null
}
insert into counter(counter,id) values(0,1)
2、计数器代码:
〈?
//php计数器,基于mysql数据库服务器。
function linuxcounter(){
//连接mysql数据库
$conn=mysql_connect(localhost,phpbook,);
//查询当前浏览数
//注意取得结果的方式
$sql=select*from counter;
$result=mysql_query($sql,$conn);
$objresult=mysql_fetch_object($result);
$count=$objresult->counter;
//更新数据库,并返回当前浏览数作为结果
$sql=update counter set counter=.($cont+1). where id=1;
mysql_query($sql,$conn);
mysql_close($conn);
return $count+1;
}