我有3个文件
如何在counter.php中显示stream.pls的总命中数? 我想知道打开/加载stream.pls的次数。
我试过了,但这不起作用:
<?php
define("COUNTER_START_VALUE", 0);
define("COUNTER_LOG", "stream.pls"); //name of file you want to use to save the counter value
/*************************************************************************************************/
function IncrementCounter()
{
$create_file = !file_exists(COUNTER_LOG);
if( !($fh = fopen(COUNTER_LOG, $create_file ? "x+b" : "r+b")) )
return "Error";
//do an flock here, maybe, I don't know :-)
//Reading current value of counter:
if($create_file)
$count = COUNTER_START_VALUE;
else
{
$count = (int)fread($fh, 9); //reads 9 digits (supposing max 1 billion count)
rewind($fh);
}
//Writing new counter value:
if(!fwrite($fh, ++$count))
return "Error";
if(!fclose($fh);)
return "Error";
return str_pad($count, 9, '0', STR_PAD_LEFT);
}
?>
答案 0 :(得分:0)
我想您要显示该文件的总访问次数
然后不要复杂,只需添加名为$ counter的变量,就像添加它一样, 然后在打开的每个文件上递增它,或者读取..
if($create_file)
$count = COUNTER_START_VALUE;
else {
fread($fh, 9);
rewind($fh);
$count++;
}
或者你可以在fopen附近使用相同的东西,
if( !($fh = fopen(COUNTER_LOG, $create_file ? "x+b" : "r+b")) )
return "Error";
else
$count++;
如果你在这个函数的开头声明$ counter ,那就更好了
function IncrementCounter()
{
$create_file = !file_exists(COUNTER_LOG);
$counter = COUNTER_START_VALUE;
.
.
.
.
我希望这会解决你的问题:) :) :)