Php 存储在.txt文件中的计数器不递增

Php 存储在.txt文件中的计数器不递增,php,html,counter,file-access,Php,Html,Counter,File Access,因此,我刚刚编辑了我发布的代码,使其更易于阅读… 第一块代码是我的计数器代码,用于点击成功提交按钮。但由于某种原因,即使提交成功,$Total的价值也不会增加1 <?php $f = fopen('count.txt', 'r+'); // use 'r+' instead $total = fgets ($f); flock($f, LOCK_EX); // avoid race conditions with concurrent requests $total =

因此,我刚刚编辑了我发布的代码,使其更易于阅读…
第一块代码是我的计数器代码,用于点击成功提交按钮。但由于某种原因,即使提交成功,$Total的价值也不会增加1

<?php 
  $f = fopen('count.txt', 'r+'); // use 'r+' instead
  $total = fgets ($f);
  flock($f, LOCK_EX); // avoid race conditions with concurrent requests
  $total = (int) fread($f, max(1, filesize('count.txt'))); // arg can't be 0
  /*if someone has clicked submit*/
  if (isset($_POST['submit'])) {
    rewind($f); // move pointer to start of file so we overwrite instead of append
    fwrite($f, ++$total);
  }
  fclose($f);
?>

发生这种情况是因为在将其写入文件之前从未设置过
$total
。 您需要通过从文件中读取其值来设置
$total
,如下所示:
$total=fgets($f)
函数调用后立即打开


但是,在没有独占文件锁的情况下,您可能会遇到并发问题,因此可能会丢失一些提交计数。

首先,在操作文件时,您可能会发现一些有用的提示:

  • 您必须始终检查文件和文件夹权限,以确保安全
  • 使用多线程代码时要小心,当多个线程同时更改一个文件时,您可能会得到意想不到的结果,所以请尝试使用锁来控制它,就像您所做的那样
我想你错过了你的标签,所以我不得不发明我自己的。 使用此代码作为指南,创建您自己的代码:

<form method="post" action="test.php">
  <input type="submit" name="submit" value="Submit" />
</form>  

<?php 
  // Thread-safe <-- Use me
  function incrementFile ($file){
    $f = fopen($file, "r+") or die("Unable to open file!");
    // We get the exclusive lock
    if (flock($f, LOCK_EX)) { 
      $counter = (int) fgets ($f); // Gets the value of the counter
      rewind($f); // Moves pointer to the beginning
      fwrite($f, ++$counter); // Increments the variable and overwrites it
      fclose($f); // Closes the file
      flock($fp, LOCK_UN); // Unlocks the file for other uses
    }
  }

  // NO Thread-safe
  function incrementFileSimplified ($file){
    $counter=file_get_contents('count.txt');
    $counter++;
    file_put_contents('count.txt', $counter);
  }

  // Catches the submit
  if (isset($_POST['submit'])) {
     incrementFile("count.txt");
  }
?>  


希望这对你有帮助!:)

我目前正在开发的网站是,如果你想了解我的意思,请尽量减少代码量或使其更丰富readable@asur因为我是新来的,我该怎么做?我认为展示所有的php脚本是很重要的……这是一个很好的实践,例如,用一个好的缩进和调试数据将逻辑和标记分开。如果你这样做,人们就更容易回答你的问题:)@Asur谢谢你的提示。下次我会这样做的D你可能知道我的代码有什么问题吗?非常感谢,现在它正在计算至少提交的数量,但我坚持要把它放到从表单设置的电子邮件中。欢迎,电子邮件是一个不同的问题,首先试着自己做,如果努力后你就是无法让它工作,发布一个新的问题,如果答案澄清了你的问题,只需勾选它,这样人们就可以找到它并解决类似的情况:)再次欢迎并记住,提问是很好的,但总是先研究类似的答案,如果你的案例不同,则以正确的格式发布,以便于人们帮助你。问候;)
<form method="post" action="test.php">
  <input type="submit" name="submit" value="Submit" />
</form>  

<?php 
  // Thread-safe <-- Use me
  function incrementFile ($file){
    $f = fopen($file, "r+") or die("Unable to open file!");
    // We get the exclusive lock
    if (flock($f, LOCK_EX)) { 
      $counter = (int) fgets ($f); // Gets the value of the counter
      rewind($f); // Moves pointer to the beginning
      fwrite($f, ++$counter); // Increments the variable and overwrites it
      fclose($f); // Closes the file
      flock($fp, LOCK_UN); // Unlocks the file for other uses
    }
  }

  // NO Thread-safe
  function incrementFileSimplified ($file){
    $counter=file_get_contents('count.txt');
    $counter++;
    file_put_contents('count.txt', $counter);
  }

  // Catches the submit
  if (isset($_POST['submit'])) {
     incrementFile("count.txt");
  }
?>