|
|
基于文件、数据库的计数器 |
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
作者:javaduke,原创于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; }
?〉

|
|
相关文章:相关软件: |
|