今天有位学弟要我给他一个网页计数器,随便写了一个,还请赐教!
<?php
// 多用户计数器
// Install:
// mkdir counter
// chmod 777 counter
// Usage:
// <SCRIPT src="http://www.YOURHOST.com/counter.php?user=YOURNAME&color=COLOR&size=SIZE&face=FACE"></SCRIPT>
// eg:
// <SCRIPT src="http://www.csdn.com/counter.php?user=ljcao&color=0000FF&size=4&face=Fixedsys"></SCRIPT>
// By Longware <longware@126.com>
// 读数据
function getData($fname)
{
$handle = fopen ($fname, "r");
$contents = "";
do
{
$data = fread($handle, 4096);
if (strlen($data) == 0)
{
break;
}
$contents .= $data;
}
while(true);
fclose ($handle);
return $contents;
}
// 写数据
function setData($fname,$value)
{
$value++;
$handle = fopen ($fname, "w");
fwrite($handle,$value);
fclose ($handle);
return $value;
}
// 主程序
$user = (isset($_REQUEST) && !empty($_REQUEST['user'])) ? $_REQUEST['user'] : "hanweb";//用户名
$color = (isset($_REQUEST) && !empty($_REQUEST['color'])) ? "#".$_REQUEST['color'] : "#000000";//颜色
$size = (isset($_REQUEST) && !empty($_REQUEST['size'])) ? $_REQUEST['size'] : "2";//字体大小
$face = (isset($_REQUEST) && !empty($_REQUEST['face'])) ? $_REQUEST['face'] : "Courier New";//字体
$fname = "./counter/".$user.".txt";
if(!file_exists($fname))
{
$num = setData($fname,0);
}
else
{
$num = setData($fname,intval(getData($fname)));
}
echo "document.write(\"<FONT COLOR='".$color."' SIZE='".$size."' FACE='".$face."'>".number_format($num)."</FONT>\");";
?>
// 多用户计数器
// Install:
// mkdir counter
// chmod 777 counter
// Usage:
// <SCRIPT src="http://www.YOURHOST.com/counter.php?user=YOURNAME&color=COLOR&size=SIZE&face=FACE"></SCRIPT>
// eg:
// <SCRIPT src="http://www.csdn.com/counter.php?user=ljcao&color=0000FF&size=4&face=Fixedsys"></SCRIPT>
// By Longware <longware@126.com>
// 读数据
function getData($fname)
{
$handle = fopen ($fname, "r");
$contents = "";
do
{
$data = fread($handle, 4096);
if (strlen($data) == 0)
{
break;
}
$contents .= $data;
}
while(true);
fclose ($handle);
return $contents;
}
// 写数据
function setData($fname,$value)
{
$value++;
$handle = fopen ($fname, "w");
fwrite($handle,$value);
fclose ($handle);
return $value;
}
// 主程序
$user = (isset($_REQUEST) && !empty($_REQUEST['user'])) ? $_REQUEST['user'] : "hanweb";//用户名
$color = (isset($_REQUEST) && !empty($_REQUEST['color'])) ? "#".$_REQUEST['color'] : "#000000";//颜色
$size = (isset($_REQUEST) && !empty($_REQUEST['size'])) ? $_REQUEST['size'] : "2";//字体大小
$face = (isset($_REQUEST) && !empty($_REQUEST['face'])) ? $_REQUEST['face'] : "Courier New";//字体
$fname = "./counter/".$user.".txt";
if(!file_exists($fname))
{
$num = setData($fname,0);
}
else
{
$num = setData($fname,intval(getData($fname)));
}
echo "document.write(\"<FONT COLOR='".$color."' SIZE='".$size."' FACE='".$face."'>".number_format($num)."</FONT>\");";
?>
在 Windows NT 5.0 build 2195 + Apache v2.0.48 + PHP v4.3.3 上运行通过。