PHP计数器代码工作出错(附关键部分源码)

来源:百度知道 编辑:UC知道 时间:2024/05/28 13:23:06
我用php写一个简单的计数器,结果工作不正常,每次刷新都是1,应该每次刷新数值增加1啊~我哪里写错了,麻烦各位帮忙看下.

counter.php代码如下:

===================================================================
<?php

$fp = fopen("counter.txt", "w+"); //打开与counter.txt的连接
$counter = fgets($fp, 80); //取80 Bytes的数据
$counter = doubleval ($counter) +1; //将数值加1
fseek($fp , 0); //将pointer指回初始位置
fputs($fp , $counter); //保存加完的数值
fclose($fp); //关闭连接

?>

--------------------------------------------------------------------

index.php代码如下:
===================================================================
<?php
include("counter.php"); //将counter.php模块嵌入网页中
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd

<?php
$fp = fopen("counter.txt", "r"); //打开与counter.txt的连接
$counter = fgets($fp, 80); //取80 Bytes的数据
$counter = doubleval ($counter) +1; //将数值加1
$fp = fopen("counter.txt", "w");
fwrite($fp , $counter); //保存加完的数值
fclose($fp); //关闭连接
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>文字版计数器</title>
</head>

<body>
<center>
<b>访问者计数器范例 - 文字版</b>