Javascript php脚本使用相同的POST数据随机重新启动

Javascript php脚本使用相同的POST数据随机重新启动,javascript,php,ajax,Javascript,Php,Ajax,我有一个php脚本,它通过xmlhttp从javascript应用程序接收解析数据 我发现php脚本有时会在处理一块数据时停止,然后使用发送给它的相同POST数据重新启动。我认为javascript的xmlhttp可能会以某种方式双重触发,但我已经用inProgress标志排除了这一点,如下所示 function SendPHP(str, callback){ xmlhttp = new XMLHttpRequest(); str = "q=" + encodeURIComp

我有一个php脚本,它通过xmlhttp从javascript应用程序接收解析数据

我发现php脚本有时会在处理一块数据时停止,然后使用发送给它的相同POST数据重新启动。我认为javascript的xmlhttp可能会以某种方式双重触发,但我已经用inProgress标志排除了这一点,如下所示

function SendPHP(str, callback){
    xmlhttp = new XMLHttpRequest();  
    str = "q=" + encodeURIComponent(str);   
    xmlhttp.open("POST","sendmail.php", true);
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState == 4){
            inProgress=false;
            if(xmlhttp.status == 200){
                            callback(xmlhttp.responseText);
            }
        }
    };
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        if (inProgress==false){
          inProgress=true;
        xmlhttp.send(str);
        }
        else{
            writeDisplayConsole("ERROR: xmlhttp fired twice!");
        }
}
如果xmlhttp尝试发送两次,则会显示该错误,并且不会再发送任何数据包。但事实并非如此,因此php脚本必须自行重新启动

请记住,它是以随机间隔发生的,而不是每次使用相同数据时都在同一时间发生。是什么导致了这一切

php脚本经常写入大文本文件日志,这可能与此有关吗

以下是php(但由于错误是在相同数据的随机间隔内发生的,我不确定php代码怎么会有问题):


你是在哪里定义的
inProgress
>?它是全局的,我已经测试过了,它可以工作。你的PHP代码是什么样子的?我会编辑它来显示PHP,一秒钟后我添加了PHP,它很长。但是如果错误发生在同一数据的不同时间,我看不出我的代码有什么问题。
<?php

writelog("\n*** Sendmail script started ***");

$q = $_POST['q'];


   //Split auth code and verify
     $pieces = explode("(<!asplit!>)", $q);
     $authc=$pieces[0];
     $q=$pieces[1];

     if ($authc<>"****"){
            echo "!Authentification denied.";
            writelog ("Authentification denied");
            die;
     }

         writelog ("Authentification accepted");




     //Split off packet details
     $pieces=explode("(<!psplit!>)", $q);
     $tpack=$pieces[0];
     $q=$pieces[1];

     $pieces=explode("-", $tpack);
     $tpacket=$pieces[0];
     $ofpacket=$pieces[1];
     $totalcontacts=$pieces[2]; 

     //Split contact data from email message
     $pieces = explode("(<!dsplit!>)", $q);
     $split=$pieces[0];
     $q=$pieces[1];

     //Assign contacts to array
     $contacts = explode ("<!", $split);
     $tcount=count($contacts);
     $tcount=$tcount-1;

     echo "(PACKET ";
     echo $tpacket;
     echo " OF ";
     echo $ofpacket;
     echo ")- ";

     writelog("(PACKET " . $tpacket . " of " . $ofpacket . ")");

         //Killswitch check incase double run
     checkKillSwitch("!Startup aborted as Power set to off.",0);
     checkKillSwitch("!Aborted, Killswitch set to Kill",1);
     file_put_contents('log/killswitch.txt', "on");

     echo $tcount;
     echo " contacts processing...";

     foreach ($contacts as &$value) {

     //check killswitch
     checkKillSwitch("Killswitch aborted during runtime",1);

                     $split=explode ("^!", $value);

            //Get the contact's details
         $firstname= $split[0];
         $lastname= $split[1];
                 $temail = $split[2];

                 if ($firstname<>""){

           $mainmessage=str_replace("[firstname]",$firstname,$q);
             $mainmessage=str_replace("[lastname]",$lastname,$mainmessage);


           //Split off subject
             $pieces = explode("(/subject)", $mainmessage);
             $tsubject=$pieces[0];
             $mainmessage=$pieces[1];

                    testLogMail($temail, $tsubject, $mainmessage);

                //log progress      
                $adder=$adder+1;

                    //For the log, show progress of total (based on 10 per packet change if different)
                    $tadder = (($tpacket-1)*10)+$adder;

                    echo ($tadder . ".");

                    writelog($tadder . " of " . $totalcontacts . " processed >> " . $temail);
                    sleep(rand(2,20));

                }   

     }

function testLogMail($xaddress, $xsubject, $xmessage){

  $tdate=date('d/m/Y H:i:s');

  $file = 'log/testmaillog.txt';
  // Open the file to get existing content
  $current = file_get_contents($file);

    // Enter email
    $towrite="To: ".$xaddress."\n";
    $towrite.="Subject: ".$xsubject."\n";
    $towrite.="Date: ".$tdate."\n";
    $towrite.="...\n";
  $towrite.=$xmessage."\n";
    $towrite.="___________________________________\n\n";

    $current .= $towrite;

  // Write the contents back to the file
  file_put_contents($file, $current);

}

 function writelog($towrite)
{
  $tdate=date('d/m/Y H:i:s');

  $file = 'log/testlog.txt';
  // Open the file to get existing content
  $current = file_get_contents($file);
  // Append a new person to the file
  $current .= $towrite." --- ".$tdate."\n";
  // Write the contents back to the file
  file_put_contents($file, $current);
} 

function checkKillSwitch($towrite, $killtype){

if ($killtype==0){
   $killswitch = file_get_contents('log/killswitch.txt');

   if ($killswitch=="on") {
    echo $towrite;
    writelog($towrite);
    die;
   }
 }

 if ($killtype==1){
   $killswitch = file_get_contents('log/killswitch.txt');

   if ($killswitch=="kill") {
    echo $towrite;
    writelog($towrite);
    die;
   }
 }


}


writelog("*** Sendmail script ended ***");
?>