views:

58

answers:

5

I have a php script that sends SMS, the problem is that it takes some time before every SMS is sent. In my site the page will wait until this script has finished running. How can I give user a message that SMS will be sent and resume the site's normal operation.

A: 

Instead or sending the SMS right there and then you can store it somewhere (for example the database). You can then build an extra script (in PHP or whatever else you want) that polls the database looking for SMS to send and dispatches them. You can have this run every x seconds or minutes via cron or scheduled task according to your OS. This way you take a time consuming task out of the page whose job is to communicate with user in a timely manner.

Manos Dilaverakis
A: 

If you are on LAMP

  1. Write a cron job that will query a queue (in database) for pending SMS and send them.
  2. In your script add the SMS to the queue.
  3. Show the user status of his SMS on another page.
  4. With Ajax you can query in background for the status of recently sent SMS. As soon as you see sent notify user.
Ankit Jain
+1  A: 

Put the messages in a queue in your database. Then have a script running as a cron job in the background to take care of the queue.

Daniel Egeberg
In principle its a good idea, but in practice using a cron job to dequeue the messages will not scale well and will cause additional, functional issues. A better solution would be a dedicated daemon - previously I'd used the BSD lpd to manage an SMS queue with a custom "printer driver" - but elsewhere used asynchronous requests to handle this
symcbean
A: 

hello ashwin, to do this you can use ajax. on some button press just send an ajax request to the php file moulded to send sms. and put a notification in the screen like 'sending sms!!!' and on the response of the ajax action change the 'sending sms' to 'success!!!'....

for using ajax you can use jquery.. if you dont know jquery comment me for the video tutorials... i have some beginners video tutorial for jquery....

Have a nice day!!!!!

Jaison Justus
But do make sure you ignore_user_abort() in the sending script if you go donn this route
symcbean
+1  A: 

The only issue here is that the browser thinks it is are waiting for more output from the script when there will be none. You could offload to a seperate process, or use an asynchronous web call, or you could simply.....

<?php
register_shutdown_function('when_alls_done');
.... // render page
exit;

function when_alls_done()
{
    if ($_REQUEST['send_to_phone']) {
       send_sms($_REQUEST['send_to_phone'], $_REQUEST['message']);
    }
}

The webserver should flush the request at the 'exit' and let the browser know that the response is complete (an explicit flush in the PHP code prior to that will either not flush the webserver buffer or it will result in the output being chunk encoded with another chunk to come).

C.

symcbean