我有一个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代码的问题):
<?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 ***");
?>