2

我只是想计算一个按钮的点击次数并将点击次数保存到一个文本文件中,但我似乎无法让这段代码工作。它不会节省点击次数。

在 HTML 文件中,我有一个运行 JavaScript 的按钮onClick

function do() {
    if (window.XMLHttpRequest){
              xhr = new XMLHttpRequest()
    }
    else 
    {
    if (window.ActiveXObject){
              var xhr = new ActiveXObject("Microsoft.XMLHTTP");
              }
              }
            xhr.open('GET','count.php',false);
            xhr.onreadystatechange = function() {
                if( xhr.readyState === 4 && xhr.status === 200 ) {
                    while(results.hasChildNodes()) {
                        results.removeChild(results.lastChild);
                    }
                    results.appendChild(document.createTextNode(xhr.responseText));
                }
            }
            xhr.send();
    }

在我们调用的 PHP 文件中,我们有以下代码:

<?php

    $clicks = file_get_contents("clicks.txt");
    $clicks++;

    $fp = fopen("clicks.txt", "w+");

    while ( !flock($fp, LOCK_EX) ) {    
        usleep(500000); // Delay half a second
    }

    fwrite($fp, $clicks);
    fclose($fp);
    flock($fp, LOCK_UN);

?>

你能帮我找出我的代码中的问题吗?
我将如何阅读另一个 HTML 页面中的文本文件?
(只显示文本文件的信息。)

4

2 回答 2

1

假设PHP页面运行正常,以下应该可以工作:

<html>
    <head>
        <title>Example</title>
    </head>
    <body>
        <script type="text/javascript">

function getXMLHttp()
{
  var xmlHttp
  try
  {
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("AJAX not supported.")
        return false;
      }
    }
  }
  return xmlHttp;
}

function MakeRequest()
{
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }
  xmlHttp.open("GET", "count.php", true); 
  xmlHttp.send(null);
}

function HandleResponse(response)
{
  document.getElementById('ResponseDiv').innerHTML = response;
}
        </script>
        <input type='button' onclick='MakeRequest();' value='Button'/>
        <br />
        <br />
        <div id='ResponseDiv'>
            Count
        </div>
    </body>
</html>
于 2012-06-22T17:19:18.430 回答
1

在 php 方面需要注意一些事项。

1) You're opening the file with w+ which is for writing and reading what you've wrote. This also truncates your file BEFORE your lock is in place.

2) Your lock will cause parallel read and writes to fail. This can set $clicks to false and possibly wipe out your counter if the file is unlocked before fopen gets called.

3) You are not correctly releasing your lock because you close the handle while the lock is active. If this happens to be a long running script the lock will not be released until the script terminates.

This should solve all of those problems.

<?php

$fp = false;
// Open file for reading, then writing
while ( ($fp=fopen('clicks.txt','r+'))===false ) {
    usleep(250000); // Delay 1/4 second
}
// Obtain lock
while ( !flock($fp, LOCK_EX) ) {    
    usleep(250000); // Delay 1/4 second
}
// Read Clicks
$clicks = trim(fread($fp,1024));
// Add click
$clicks++;
// Empty file
ftruncate($fp,0);
// Write clicks
fwrite($fp, $clicks);
// Release Lock
flock($fp, LOCK_UN);
// Release handle
fclose($fp);

?>
于 2012-06-22T17:47:08.183 回答