文档介绍:PHP应用实例
计数器(1)
计数器是计算网站中某一个或几个页面被访问的次数。计数器可以用两种方法来实现—文件和数据库。
通常,先设计一个计数器的公共文件,用来访问存储数据的文件,其它的需要计数的文件用require函数包括公共文件就可以了。
计数器(2)
<?//
function MyCounter() {
$counterFile="c:\\temp\\";
if (!file_exists($counterFile)) {
if (!file_exists(dirname($counterFile))) {
mkdir(dirname($counterFile), 0700);
}
exec("echo 0 > $counterFile");
}
计数器(3)
$fp = fopen($counterFile,"rw");
$num = fgets($fp,5);
$num += 1;
print "$num";
exec("del/q $counterFile");
exec("echo $num > $counterFile");
}
?>
计数器(4)
统计数据文件:
<?
require("");
?>
<html>
<head>
<title>计数器举例</title>
</head>
计数器(5)
<body>
您是第<? MyCounter(); ?> 位参观者
</body>
</html>
略
投票系统(1)
use mytest;
create table ballot(
itemid tinyint unsigned not null,
ballotsum smallint unsigned
);
投票系统(2)
insert into ballot(itemid,ballotsum)values(1,0);
insert into ballot(itemid,ballotsum)values(2,0);
insert into ballot(itemid,ballotsum)values(3,0);
初始化数据。
投票系统(3)
<?
if(isset($ballot)){
$bid=$HTTP_COOKIE_VARS["bid"];
if($bid==""){
mt_srand((double)microtime()*1000000);
$bid=crypt(uniqid(mt_rand()));
setcookie("bid",$bid,time()+3600);
$mysql_link=mysql_connect("localhost","","");
mysql_select_db("mytest",$mysql_link);
投票系统(4)
$query="update ballot set ballotsum=ballotsum+1 where itemid=$ballot";
mysql_query($query,$mysql_link);
echo "投票完成!<br>\n";
}
else{
echo "你已经投过票!";
}
}
?>