需要帮助重置php计数器

时间:2012-02-16 17:44:38

标签: php

我是php的新手。我决定根据我见过的剧本制作一个计数器。我已经对它进行了修改。我正在试图找出如何重置计数器。

$userCount = file_get_contents("count.txt");

$userCount = trim($userCount);

$userCount = $userCount + 1;

$countReset = $userCount - $userCount;

$file = fopen("count.txt","w+");

fwrite($file,$userCount);

fclose($file);

print "The number of visitors is: $userCount";

if ($userCount < 20){
echo 'not yet';
}
else {
echo 'done!';
}

if ($userCount > 40){
fwrite($file,$countReset);
fclose($file);
}

我尝试从自身中减去计数器,使其恢复为0。

$countReset = $userCount - $userCount;

然而,它似乎不起作用。计数器本身有效,但我无法将其重置为0。

这只是一种印象,就像我正在做的一种学习php的方式。另外,对于格式感到抱歉,与这个帖子编辑一起努力。

有关脚本的任何帮助吗?

6 个答案:

答案 0 :(得分:2)

在尝试再次写入文件之前,您已关闭该文件。在第二个fwrite之前,添加:

$file = fopen("count.txt","w+");

答案 1 :(得分:1)

尝试简单地将值0设置为var:

$countReset = 0;

答案 2 :(得分:1)

你不能只编辑 count.txt 文件吗?

在PHP中完成它,你可以做到

fwrite($file,'0');
编辑:就像CanSpice所说,你不应该在完成之前关闭文件。删除第一个fclose,它应该可以工作。

答案 3 :(得分:1)

/****************************************************************************
 * read the current count from the counter file, increment
 * it by 1, and re-save the new value off to the file
 ***************************************************************************/
function getAndIncrementCount(){
  // attempt to open the file
  // a+ means keep the contents so we can read it at least once,
  // but allow us to overwrite the value once we increment it
  if (($fHandle = fopen('count.txt','a+')) !== FALSE){
    // read in the count (also cast to an int so if there is
    // an invalid (or absent) value it will default to a 0
    $count = (int) fread($fHandle, 100);

    // increase the counter
    $count++;

    // go back to the beginning of the file
    fseek($fHandle, 0);

    // re-write the new count back to the file
    fwrite($fHandle, $count);

    // cose the file now that we're done with it
    fclose($fHandle);

    // return back the count
    return $count;
  }

  // we couldn't get to the file, so return an error flag
  return FALSE;
}

/****************************************************************************
 * write the specified value to the counter file
 ***************************************************************************/
function setCount($count = 0){
  // attempt to open the file with over-write permissions
  // w+ will open the file and clear it
  if (($fHandle = fopen('count.txt','w+')){
    // write the counter to the file
    fwrite($fHandle, $count);

    // close the file now that we're done
    fclose($fHandle);

    // return the newly saved count
    return $count;
  }

  // we couldn't get to the file, so return an error flag
  return FALSE;
}

并在实践中应用:

$userCount = getAndIncrementCount();
echo "The number of visitors is: {$userCount}";

if ($userCount < 20){
  echo "Not Yet";
}else{
  echo "Done!";
}
if ($userCount > 40){
  setCount(0);
}

答案 4 :(得分:1)

我不会在此上下文中混用fopen()file_get_content()个函数,使用fopen()fread()fwrite()或使用file_get_contents()file_put_contents()

如果你只需要重置计数器而你不需要以前的值,那么使用:

file_put_contents('count.txt', '0');

如果您需要更新价值,可以使用:

$count = file_get_contents( 'count.txt');
$count++;

// Reset?
if( $reset){
    $count = 0;
}

file_put_contents( 'count.txt', "$count");

或者更确切地说:

$fp = fopen( 'count.txt', 'r+') or die( 'Cannot use counter');
$count = trim( fread( $fp, 1024));
$count++;

// Reset?
if( $reset){
    $count = 0;
}

ftruncate( $fp, 0);
fseek( 0, SEEK_SET)
fwrite( $fp, "$count");
fclose( $fp);

以下是ftruncate()fseek() +的手册页,你应该研究flock(),这样两个脚本就不会同时覆盖这些内容。

答案 5 :(得分:0)

这是因为您没有重写文件内容,而是在第二次使用fwrite时添加它们,因此$ countReset会附加到文件中已有的内容中。试试这个:

$userCount = file_get_contents("count.txt");
$userCount = $userCount + 1;

$countReset = $userCount - $userCount;

file_put_contents("count.txt", $userCount);

print "The number of visitors is: $userCount\n";

if ($userCount < 20){
    echo 'not yet';
}
else {
    echo 'done!';
}

if ($userCount > 40){   
    file_put_contents("count.txt", $countReset);
}