简体   繁体   中英

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.

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.

so here is the ajax part:

<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

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

Tech support of the hosting changed php version from 5.2 to 5.3 and now it works. It works and seems to run faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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