简体   繁体   English

PHP上的错误500 for循环

[英]Error 500 on php for loop

I am making a little script that read emails from my inbox and save them in a mysql database. 我正在编写一个小脚本,该脚本从收件箱中读取电子邮件并将其保存在mysql数据库中。

I have a php that check emails and save them to DB, and break the FOR if it ran for more than 1 second, and I have an ajax script that call the php every time it stops. 我有一个检查电子邮件并将其保存到数据库的php,如果它运行了1秒钟以上,则破坏FOR,并且我有一个ajax脚本,该脚本每次停止都会调用php。

so here is the ajax part: 所以这是ajax部分:

<script type="text/javascript">
$(document).ready(function(e) {
chequearCorreo(1);
});

function chequearCorreo(inicio){
$.ajax({
  type: 'POST',
  url: 'inc/a.php',
  data: {mailInicio: inicio},
   success: function(data) {
       resultado = data.split("/");
       if(resultado[0]!=resultado[1]){
           //aun no termino
           chequearCorreo(resultado[0]);
       }
       $("body").html(data);

    },
    error: function(data){

    },
 contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
 dataType: 'html'
});
}
</script>

and here is the php 这是PHP

session_start();
include 'coneccion-base-mails.php';

$crear_tabla_web =
'CREATE TABLE IF NOT EXISTS web  
(
id INT NOT NULL AUTO_INCREMENT,
uid int,
carpeta VARCHAR(200),
fecha DATETIME,
remitente VARCHAR(200),
destinatario VARCHAR(200),
cc VARCHAR(200),
cco VARCHAR(200),
asunto VARCHAR(200),
body TEXT,
adjuntos VARCHAR(200),
leido VARCHAR(20),
flaged VARCHAR(20),
respondido VARCHAR(20),
reenviado VARCHAR(20),
PRIMARY KEY(id)
)';
$mysqli->query($crear_tabla_web);

//FUNCION GUARDAR MAIL!!!

function guardarMail($uid,$carpeta,$fecha,$remitente,$destinatario,$cc,$cco,$asunto,$body,$leido,$flaged,$respondido,$reenviado){
global $mysqli;
//primero chequeamos que el mail no exista ya en la base
if ($result=$mysqli->query("SELECT * FROM web WHERE uid=".$uid."")){
    if( $result->num_rows == 0 ){
        //guardamos el post
        $fecha = strtotime($fecha);
        $fecha = date("Y-m-d H:i:s", $fecha);

        $cons  = "INSERT INTO web (uid,carpeta,fecha,remitente,destinatario,cc,cco,asunto) VALUES (".$uid.",'".$carpeta."','".$fecha."','".$remitente."','".$destinatario."','".$cc."','".$cco."','".$asunto."')";
        $mysqli->query($cons);
    }
}
}
//fin guardar mail

$hostname = '{localhost:143}Inbox';
$username = '##########';
$password = '###';


$tiempoInicio = microtime(true);
/* Intento de conexión */
$conn = imap_open($hostname,$username,$password) or die('No se pudo conectar con: usuario: '.$username.' y clave: '.$password.' ' . imap_last_error());

$numMsg = imap_num_msg($conn);

for($i=$mailInicio;$i<=$numMsg;$i++){

$header = imap_header($conn,$i) ;
$fromInfo = $header->from[0];
$replyInfo = $header->reply_to[0];
$toInfo = $header->to[0];
$ccInfo = $header->cc[0];
$bccInfo = $header->bcc[0]; 

$detalles = array(
    "mailRemitente" => (isset($fromInfo->mailbox) && isset($fromInfo->host))
        ? $fromInfo->mailbox . "@" . $fromInfo->host : "",
    "nombreRemitente" => (isset($fromInfo->personal))
        ? $fromInfo->personal : "",
    "mailDestinatario" => (isset($toInfo->mailbox) && isset($toInfo->host))
        ? $toInfo->mailbox . "@" . $toInfo->host : "",
    "nombreDestinatario" => (isset($toInfo->personal))
        ? $toInfo->personal : "",
    "mailRespuesta" => (isset($replyInfo->mailbox) && isset($replyInfo->host))
        ? $replyInfo->mailbox . "@" . $replyInfo->host : "",
    "nombreRespuesta" => (isset($replyTo->personal))
        ? $replyto->personal : "",
    "mailCc" => (isset($ccInfo->mailbox) && isset($ccInfo->host))
        ? $ccInfo->mailbox . "@" . $ccInfo->host : "",
    "nombreCc" => (isset($ccInfo->personal))
        ? $replyto->personal : "",
    "mailCco" => (isset($bccInfo->mailbox) && isset($bccInfo->host))
        ? $bccInfo->mailbox . "@" . $bccInfo->host : "",
    "nombreCco" => (isset($bccInfo->personal))
        ? $bccInfo->personal : "",
    "asunto" => (isset($header->subject))
        ? $header->subject : "",
    "fecha" => (isset($header->udate))
        ? $header->udate : ""
);

$uid = imap_uid($conn,$i);
guardarMail($uid,'Inbox',$detalles["fecha"],$detalles["mailRemitente"],$detalles["mailDestinatario"],$detalles["mailCc"],$detalles["mailCco"],$detalles["asunto"],'','','','','');

$tiempoActual = microtime(true);

$deltaTiempo = $tiempoActual - $tiempoInicio;


if($deltaTiempo > 1 ){


    break;

}



}
echo $i.'/'.$numMsg;

the problem is that the script runs for about 5 times, but at somepoint the ajax can't load the php anymore, it gives error 500 问题是该脚本运行了大约5次,但是在某个时刻ajax无法再加载php,它给出了错误500

Tech support of the hosting changed php version from 5.2 to 5.3 and now it works. 托管的技术支持将php版本从5.2更改为5.3,现在可以使用了。 It works and seems to run faster. 它有效,并且运行速度似乎更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM