Current location: Hot Scripts Forums » Programming Languages » PHP » Is there a way to delay a PHP Script?


Is there a way to delay a PHP Script?

Reply
  #1 (permalink)  
Old 03-25-06, 09:40 AM
KeYBLeR KeYBLeR is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
Is there a way to delay a PHP Script?

Hello again everyone, I just wanted to say thanks for all your help so far.

I need to know how to delay a php script.

Here is my php script that sends a email message to every person in my newsletter.

PHP Code:

if($_GET['sendemail'] == "sendall"){

$email $_POST['email'];
$subject $_POST['subject'];
$message $_POST['message'] . "\n\n\n$unsubscribe_email_message";
$result mysql_query("SELECT * FROM list");
while(
$row mysql_fetch_array($result)) {
include(
"../config_newsletter.php");
$date date ("l, F jS, Y"); 
$time date ("h:i A"); 
$id "$row[id]";
$name "$row[name]";
$email "$row[email]";
mail ("$email""$subject""$message""From: $admin_email\r\nReply-to:$admin_email");
echo(
"eMail Sent to: $name at, <a href=\"mailto:$email\">$email</a><br>");
}
echo(
"<hr color=#000000>This eMail Was Sent!");

This is the part I would like to delay, (I Think)
PHP Code:

$result mysql_query("SELECT * FROM list");

while(
$row mysql_fetch_array($result)) { 
This is where it starts pulling emails from my mysql database and starts emailing them.

Is there a way I can delay the script xx amount of time. This way It won't use up the servers load. Say theres 2,000 emails in my list, and I'm trying to email them back to back, Thats using up alot of resources.

Thanks Keith
Reply With Quote
  #2 (permalink)  
Old 03-25-06, 11:39 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Add the line in bold:

if($_GET['sendemail'] == "sendall"){
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'] . "\n\n\n$unsubscribe_email_message";
$result = mysql_query("SELECT * FROM list");
while($row = mysql_fetch_array($result)) {
include("../config_newsletter.php");
$date = date ("l, F jS, Y");
$time = date ("h:i A");
$id = "$row[id]";
$name = "$row[name]";
$email = "$row[email]";

sleep(1);

mail ("$email", "$subject", "$message", "From: $admin_email\r\nReply-to:$admin_email");
echo("eMail Sent to: $name at, <a href=\"mailto:$email\">$email</a><br>");
}
echo("<hr color=#000000>This eMail Was Sent!");
}


Be aware your server may time out during a long run like this.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #3 (permalink)  
Old 03-25-06, 11:45 AM
KeYBLeR KeYBLeR is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
Any suggestions on how to not make it time out?

I have a great server, nerver any problems, its on a 21 gbps backbone internet connection (if that matters).

Will it time out because the server hardware?
Or will it time out because of script issues?


Thanks Keith
Reply With Quote
  #4 (permalink)  
Old 03-25-06, 11:46 AM
KeYBLeR KeYBLeR is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
what does sleep 1 equal? 1 minute?

Keith
Reply With Quote
  #5 (permalink)  
Old 03-25-06, 12:28 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Reply With Quote
  #6 (permalink)  
Old 03-25-06, 05:43 PM
wiremind wiremind is offline
Newbie Coder
 
Join Date: May 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by KeYBLeR
Any suggestions on how to not make it time out?
After you delay your script with "sleep" just reload the page like this
Code:
header("location: index.php")
where index.php is the name of your file
__________________
Online Dating Software
Reply With Quote
  #7 (permalink)  
Old 03-25-06, 05:55 PM
Pineriver Pineriver is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
"set_time_limit(SECONDS)"
The max exicution time is 30 seconds without using this function, afterwich you would get an error.

If you are going about the sleep() function and have thousands of emails to process this would take a long time, so you might want to set the ini setting that you can enable to keep the script working even if the user terminates closes/gose to another page. I dont know the exact code for this tho, maybe someone will point it out.
__________________
Web PHP Programmer - http://vmist.net
Reply With Quote
  #8 (permalink)  
Old 03-25-06, 06:49 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
I's also flush the buffer each time- this would more than likely prevent timeouts. I have some scripts that run for hours, even with the default 30-second ini value. As long as data keeps getting sent to the browser it can run for as long as you need it to.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #9 (permalink)  
Old 03-25-06, 08:27 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Hi,

I wouldn't worry about the server capacity. The email software is very sophisiticated. If it gets too many emails to send, it will queue them up. Don't reinvent the wheel, trust your email system.

Set it up to run as a cron job at 2am and blast them out all at once.

Have it send you an email when it completes, and have it send the cron output (whatever you want it to be).

If there is a problem - you should be able to recover by "normal business hours".

Obviously, this advice is given with a . I have been responsible for ensuring email software runs properly and one of my first rules is to run large lists in the middle of the night.

Good luck.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking to buy an original PHP script for resale RenFromPenn Job Offers & Assistance 0 07-19-05 02:36 PM
PHP script problem (please help) osmanmumtaz PHP 0 05-24-05 07:29 AM
PTS (Paid-To-Search) PHP Script Punniabi Script Requests 1 05-23-05 11:28 PM
PHP Membership Script sixflagsga Script Requests 1 05-19-05 05:23 PM
Need Custom PHP Script kellyk Script Requests 2 11-13-04 06:17 PM


All times are GMT -5. The time now is 07:05 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.