Image

Imagemister_lance wrote in Imagephp

How to create a timeout in the php script?

Hello,

I was wondering if there was an easy way to create a script time out to where if a particular script continues to run for an extended period of time, less than max_execution_time in the php.ini, it will exit itself.

The problem which supersedes the query above is that I have an AJAX request that connects to my mailform.inc.php file which figures out which case AJAX is requesting, and then performs the specified case... my AJAX object has a timeout procedure which seems to work just fine; however, when I send a subsequent request to the server, which uses the same mailform.inc.php file, the AJAX timeout is reached so I'm assuming that the mailform.inc.php file is still held up somewhere.

The bottom line is that if my AJAX request encounters a timeout then I want my php script to release as well, which it doesn't seem to be doing.

EDIT: I actually found the cause of the php file hanging up... the smtp connection required ssl/tls but I didn't handle the database information correctly--I formatted it for output in the mail settings which would have checked a check box instead of altering the smtphost variable to include the ssl:// prefix. I suppose the solution to my overall problem is to capture any errors from the myMail function generated through PEAR during the request and output those... otherwise, who knows?


Here is the pertinant PHP code from the mailform.inc.php file:


case "sendmail":

if($_POST['to']){

if($row = mysql_fetch_array(getEmailSettings($_SESSION['username']))){


@$smtphost = $row['smtphost'];
@$smtpport = $row['smtpport'];
@$mail_username = $row['mail_username'];
@$mail_password = $row['mail_password'];
@$to = strip_tags($_POST['to']);
@$subject = stripslashes(strip_tags($_POST['subject']));
@$body = stripslashes(wordwrap(strip_tags($_POST['body'])));

if($row['is_ssl'] ==1){

$ssl = "checked=\"checked\"";

}else{

$ssl = "";
}
}
if(myMail($smtphost, $smtpport, $mail_username, $mail_password, $mail_username, $to, $subject, $body)){

echo "Success email sent, ok";

}else{

echo "Success email not sent";
}
}


break;


Here is the code from the myMail function:


function myMail($host, $port, $username, $password, $from, $to, $subject, $body){
//$host = "ssl://smtp.gmail.com";
//$port = 465;
//$username = "";
//$password = "";

//$from = "";
//$to = "";
//$subject = "Hi!";
//$body = "Hi,\n\nHow are you?";



$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo("

" . $mail->getMessage() . "

");
return false;
} else {
echo("

Message successfully sent!

");
return true;
}
}


and here is the AJAX code which handles the timeout:


var t = window.setTimeout(function(){
if(that.updating){
that.updating=false;
that.AJAX.abort();
that.AJAX=null;
delete that.AJAX;
that.callback("An Error occured; too much traffic. Please try again later. ", 200, null, DivSpan);}},10000);