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.
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.
If you are on LAMP
- Write a cron job that will query a queue (in database) for pending SMS and send them.
- In your script add the SMS to the queue.
- Show the user status of his SMS on another page.
- With Ajax you can query in background for the status of recently sent SMS. As soon as you see sent notify user.
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.
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!!!!!
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.