0

Possible Duplicate:
Best way to manage long-running php script?

I have to built a big email list.Everything works perfectly,but when i submit the form page is loading untill every email is send.So i want this email sending script run in background.and notice the user that script is runnign in background. I cant use Ajax.

i want something like.. proc_open,exec,shell_exec..

Community
  • 1
  • 1
Alfred Francis
  • 451
  • 1
  • 6
  • 20

8 Answers8

1

You can have cron job which would run php script which will get queue from db and send email

On main script you just need to add emails to queue

I would use ajax only if you need progress bar. With ajax solution you would need to keep window open until it's ended.

Jerzy Zawadzki
  • 1,975
  • 13
  • 15
0

You could build an AJAX call that calls the php script. This way, your site will still be operational while the request is fulfilled. And when it's finished, your AJAX will return and you can show a messagebox to the user.

For more information, check at least this and if you understand what AJAX is and what it does, use it with this

F.P
  • 17,421
  • 34
  • 123
  • 189
0

Ajax request would be the best choice for this. You can send a request using javascript and even report progress to user (which might require some additional work)

If you find ajax too difficult - run script in an iframe. This is not the most elegant, but the most simple method.

Silver Light
  • 44,202
  • 36
  • 123
  • 164
0

Submit the form with AJAX and update the progress in a Div

Prabhuram
  • 1,268
  • 9
  • 15
0

For example - write to some place "A"(db or file) current state of your script runtime: "complete"/"incomplete". After start script in background send to your user waiting page which using AJAX handling changes at "A".

SoWa
  • 314
  • 4
  • 12
0

This Ajax script will execute a PHP file on the background. It could also send the response to a HTML element if you want.

<script type="text/javascript" language="javascript">

function execute(filename,var1,var2,var3)
{
var xmlhttp;
if(window.XMLHttpRequest)
{
    //Code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
    //Code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
    alert("Your browser does not support AJAX!");
}

    var url = filename+"?";
    var params = "var1="+var1+"&var2="+var2+"&var3="+var3;

    xmlhttp.open("POST", url, true);

xmlhttp.onreadystatechange=function()
{
    if(xmlhttp.readyState==4)
    {
        //Below line will fill a DIV with ID 'response'
        //with the reply from the server. You can use this to troubleshoot
        //document.getElementById('response').innerHTML=xmlhttp.responseText;

        xmlhttp.close;
    }
}

    //Send the proper header information along with the request
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");

    xmlhttp.send(params);
 }
 </script>
Pravitha V
  • 3,308
  • 4
  • 33
  • 51
0

You can try to run the script through an ajax function as well if you don't want to set cron script.

Debashis
  • 566
  • 2
  • 14
  • 34
0

PHP has an function that can keep an process running even if the user that requested the page leaves the page : ignore_user_abort if you check the comments there you can see this example :

<?php
ignore_user_abort(1); // run script in background
set_time_limit(0); // run script forever
$interval=60*15; // do every 15 minutes...
do{
   // add the script that has to be ran every 15 minutes here
   // ...
   sleep($interval); // wait 15 minutes
}while(true);
?>

It IS an pure php cron job BUT, the risk with this script is that it continues indefinitely or atleast untill you reset/kill php.

Setting the set_time_limit(0); to set_time_limit(86400); would kill the script after an day.

This should point you in the right direction/.

IMPORTANT After the problem by the OP, it is advisable to only run this script if you have SSH access to the server so you can KILL/RESTART php apache in case the server keeps hanging. Also do not run the script on a LIVE server.

DonSeba
  • 2,098
  • 2
  • 16
  • 25