Jump to content

Delayed PHP script


Demonwolf
 Share

Recommended Posts

HI all I hope someone can help me. I am using XAMPP and hosting an internal support website. With help from W3Schools and various other online pages, I have put together something quite simple. A user enters information on a page that is set out as follows:

<?php session_start();$_SESSION["Valid"] = true;?><html><head>  <script type="text/javascript">   function validateForm()   {	var sFName=document.forms["fQuery"]["sName"].value;	var sEma=document.forms["fQuery"]["sEmail"].value;	var sLoca=document.forms["fQuery"]["sLocation"].value;	var sProb=document.forms["fQuery"]["sProblem"].value;	var atpos=sEma.indexOf("@");	var dotpos=sEma.lastIndexOf(".");	var blFormChk = true;	if (sFName==null || sFName=="")	 {	 alert("Name must be filled in");	 blFormChk = false;	 }	if (sEma==null || sEma=="")	 {	 alert("A valid email address must be filled in");	 blFormChk = false;	 }	if (checkEmail(sEma) == false)	 {	 alert("Not a valid e-mail address");	 blFormChk = false;	 }	if (sLoca==null || sLoca=="")	 {	 alert("Please give the location");	 blFormChk = false;	 }	if (sProb.length < 30)	 {	 alert("Please submit a more detailed query");	 blFormChk = false;	 }	return blFormChk;   }   function checkEmail(checkStr)   {	var blValid = false;	for (var i=0; i<checkStr.length; i++)	{	 var chAtI = checkStr.charCodeAt(i);	 if ((chAtI<45) || (chAtI==47) || ((chAtI>57) && (chAtI<64)) || ((chAtI>90) && (chAtI<95)) || (chAtI==96) || (chAtI>122))	 { blValid = false; }	 else { blValid = true; }		 if (blValid == false) { break; }	}	if (blValid == true)	{	 sConv = checkStr.split("@");	 if (sConv.length == 2)	 { blValid = true;}	 else	 { blValid = false; }		 if (blValid == true)	 {	  if ((sConv[0].length > 0) && (sConv[1].length > 0))	  { blValid = true; }	  else	  { blValid = false; }	 }		 if (blValid == true)	 {	  sDom = sConv[1].split(".");	  if (sDom.length==1)	  { blValid = false; }	  else	  {	   for (var j=0; j<sDom.length; j++)	   {		if (sDom[j].length > 0)		{ blValid = true; }		else		{		 blValid = false;		 break;		}	   }	  }	 }	}	return blValid;   }  </script><title>Support Query</title><link rel="shortcut icon" href="/favicon.ico" /></head><body style="background-color:#000066;color:#FFFFFF">  <h1>Company</h1>  <h2>Submit a Support Query</h2>  <table border=0>   <form name="fQuery" action="QueryList.php" onsubmit="return validateForm()" method="post">	<tr>	 <td>	  <p>* Required fields</p>	  <br />	  * Name:	  <br />	  <input type="text" name="sName" size="60" maxlength="120" value=""/>	  <br />	  * E-mail address:	  <br />	  <input type="text" name="sEmail" size="60" maxlength="120" value="" />	  <br />	  Where to find you or contact number:	  <br />	  <input type="text" name="sContact" size="60" maxlength="120" value="" />	  <br />	  * Location related to query (eg. Room 23, Staffroom):	  <br />	  <input type="text" name="sLocation" size="60" maxlength="180" value="" />	  <br />	  Type of device related to query (eg. Projector, Sound Box, Computer):	  <br />	  <input type="text" name="sDevice" size="60" maxlength="180" value="" /><br />	  <br />	  * Query (Please give details): <br />	  <textarea name="sProblem" cols='60' rows='6' onkeypress="nCount.innerHTML=250-sProblem.value.length" onkeyup="nCount.innerHTML=250-sProblem.value.length" onkeydown="nCount.innerHTML=250-sProblem.value.length"></textarea>	  <label for="sProblem" id="nCount">250</label>	</tr>	<tr>	 <td>	 <br />	 Please only click submit ONCE and wait for your ticket number.<br />  	 It may take up to a minute for a confirmation page to be displayed.	 </td>	</tr>	<tr>	 <td>	  <br />	  <input type="submit" value="Submit"/>	 </td>	</tr>   </form>  </table></body></html>

Then they click the submit button and the information goes to the next page:

<?php session_start();?><html><head>  <link rel="shortcut icon" href="/favicon.ico" /></head><body style="background-color:#000066;color:#FFFFFF"><?php  $sFileDate = Date("Ym");  $fDataFile = "./Datafiles/".$sFileDate.".csv";  $fQueries = fopen($fDataFile,"a");  $nCounter = count(file($fDataFile));  if ($nCounter == 0)  {   fwrite($fQueries,("TicketNo,Name,Email,ContactNo,Location,Device,Query"));   $nCounter = 1;  }  $sPost = vsprintf("%'04s",$nCounter);  if ($_SESSION["Valid"] === true)  {  $_SESSION["Valid"] = false;  $aData = array(Date("Ym"),$sPost,",",str_ireplace(",",";",$_POST["sName"]),",",str_ireplace(",",";",$_POST["sEmail"]),",",str_ireplace(",",";",$_POST["sContact"]),",",str_ireplace(",",";",$_POST["sLocation"]),",",str_ireplace(",",";",$_POST["sDevice"]),",","\"".(str_ireplace(array("\n","\r","\r\n")," ",str_ireplace(",",";",$_POST["sProblem"])))."\"");  fwrite($fQueries,("\r\n".implode($aData)));  fclose($fQueries);  set_time_limit(60);  mail("//data has been removed");  set_time_limit(60);  mail("//data has been removed");  } else {$sPost = vsprintf("%'04s",$nCounter-1);}?><h1>Thank you</h1><p>Your support ticket number is <h2><b><?php print_r(Date("Ym").$sPost);?></b></h2>This ticket number has been emailed to: <?php print_r($_POST["sEmail"]);?></p><p>An email has been sent to support with your query. You should be contacted soon.</p></body></html>

And it all works wonderfully... except that the two mail() entries take a while to send. This appears to be causing complications. Before I had the "set_time_limit(60);" lines in, 30seconds in and the script would terminate with an error that the users see. The time limits now stop the error but the user can wait up to 1:59s before it displays the page. Anything beyond that and it gives an error. Is there a way I can display the output page, giving them their reference number while running the mail sending in the background? And if they try close the page, it should give a message saying that it is still sending the mails? Your help would be greatly appreciated.

Edited by Demon Wolf
Link to comment
Share on other sites

It could be a couple of things. You aren't using the mail functions correctly. What exactly are you trying to accomplish by using it that way? There is documentation for using it correctly.http://www.w3schools.com/php/php_mail.asphttp://php.net/manual/en/function.mail.php Of course this goes without saying, but is important to note:

Note: For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file. Read more in our PHP Mail reference.
Link to comment
Share on other sites

The mail function shouldn't take that long, but if you would rather have PHP run in the background instead of troubleshoot sending mail, check the user notes on this page: http://www.php.net/manual/en/features.connection-handling.php Note that you'll need to use output buffering, and your email code goes after all HTML that needs to be sent since you will disconnect the browser before sending the mail.

Link to comment
Share on other sites

XAMPP has the sendmail program which allows SMTP authentication, which the normal mail() in PHP does not support. I have updated to the latest version and included the dlls that allow TLS authentication. Here is my PHP.ini [mail function] section

[mail function]sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"mail.add_x_header = Off

And in the sendmail we have:

[sendmail]smtp_server=pod*****.outlook.comsmtp_port=587smtp_ssl=tlserror_logfile=error.logdebug_logfile=debug.logauth_username=no-reply@//our domain nameauth_password=***force_sender=no-reply@//our domain name

It sends the mail so there is no complication with it, except it takes a while. I think it is related to the fact that we do not have an internal SMTP and the one online required authentication, which can take some time. Our mail lines look as follows

mail("support@//our domain name","Support Ref #".Date("Ym").$sPost,wordwrap($_POST["sName"]."\n\nContact details: \n".$_POST["sEmail"]."\n".$_POST["sContact"]."\n\nHas submitted a query with regards to the ".$_POST["sDevice"]." in ".$_POST["sLocation"]."\n\n".$_POST["sProblem"],70),"From: Query Submission <no-reply@//our domain name>");  mail($_POST["sEmail"],"Your support Ref #".Date("Ym").$sPost,wordwrap("Your support reference code is ".Date("Ym").$sPost."\n\nThank you and someone should be in contact soon.",70),"From: Query Submission <no-reply@//our domain name>");

Sorry for all the "bleeping out", company policy is a pain sometimes. I am looking into the pages. The connection handling looks like it holds the most promise. I just hope I can figure it out.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...