Jump to content

prevent time out script


cssfreakie

Recommended Posts

Hi all,

 

I am trying to make a emailscript with PEAR to send quite some people a personalized messaged. But i thought this could very well cause the script to time out. Now i read http://php.net/manual/en/function.set-time-limit.php, but i thought does maybe someone knows a way to prevent a time out instead of setting the time-limit to a larger amount.

Some terms i saw floating around were: ob_start, flush, sleep and a few others but i really never worked with these.

If someone could point me in the right direction of thinking or maybe knows a tutorial or guide i really would love to hear it. Thank you!  8)

Link to comment
Share on other sites

Fork it as a background process. You should not wait for things like that to complete in a web browser. Send it straight to the command line as follows:

exec("php /path/to/mail-script.php > /dev/null &");

You could get the script to set a flag in your database once it starts to say that a mailer is being sent out. You can use this flag to display a message in the browser or disable buttons so another mailer cannot be sent until the current script has completed.

Link to comment
Share on other sites

Hi Neil, thanks for your swift reply.

 

I am not yet that experienced, but i am trying to understand what you just wrote.

 

By using that exec() function i call the mailer script in the command-line outside the browser. inside the mailerscript i make a call to a database to set a flag. May i assume i record the time or something that is has been sent.

 

I am not really sure what to do with that flag. If you happen to have some spare time would you mind explaining this technique a bit more, or maybe point me to the right direction. I am pretty inexperienced with these advanced things.

 

Thanks for your reply

 

-edit, doesn't this allow some extra security holes?

Link to comment
Share on other sites

Well what i initially wanted is have a mailing script that

emails a personal message to all users and every time a email is sent it says for instance:

 

 

* email succesfully sent to ....
* email succesfully sent to ....
* email succesfully sent to ....

I saw such an email component and I thought that would be great if i could make that.

 

I thought i might use that flush() stuff for it or ajax. This exec() function is completely not understandable for me yet since i don't know how to write in commandline. besides that i am running wamp on windows and google shows loads and loads of people having troubles with exec on windows. that exec is just to advanced for me at the moment certainly because it will run something in the background without me knowing what is happening, i should look at that in a few months when i know a little more about php. i am still a noobie  :o

 

I will puut that set_time_limit to 0 and see what happends. :)

Link to comment
Share on other sites

OK, the solution you opt for depends on the number of messages that you will be sending. I have mail scripts that send newsletters to thousands of users. I do not want to see this running on screen printing a message for every email sent so what I opt for is a button within the CMS that the user can click. This will start a PHP script in the background that sends the newsletter or mailer out. Part of the script will log to a database (or text file) what it is doing so I can check where it is upto. As this data is being recorded I can add code to my CMS that does not allow the user to send another batch of emails until the current message has been sent to all subscribers. Once the script has completed I get it to send an email to the administrator to say it is complete.

 

If you are sending to a smaller number of users and do want the progress to be displayed on the screen as the script runs, there is no problem in setting the max execution time via a .htaccess file or using the set_time_limit() function. As an alternative to writing your own code to display the progress using output buffering or AJAX, I would opt for an API that already has this functionality built in. One that I have used many times is Swiftmailer http://swiftmailer.org/

 

One downside to this is that if a user was to close the browser window, click off the page for any reason whilst the script is running it would terminate and you may not know what emails have been sent to your users (unless you are recording in a database table). This is not the case when a script is initiated in the background. A command line script is absolutely no different to what you would write to be output in a web browser, the only difference is that you do not need any output to be displayed, the code you write just does a particluar job and that is it, no HTML, no CSS, nothing except PHP.

 

How you implement the mail system is entirely upto you.

 

http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time

/* top of script */
set_time_limit(0);

Link to comment
Share on other sites

Thanks alot neil for this detailed response. I agree with you that a background process would be ideal. I will certainly have a look into that. But i still need to learn alot. So i put that on my to do list. Would you mind if i bother you ones i made a complete script with that exec() functionality in it and the flag to the database just to have a quick look. I think i now have a better Idea of what you meant and i will try to work it out as far as my knowledge reaches.

Anyways thanks alot for your reply and help and also Crayon thanks alot for your help.

 

Cheers!

8)

 

-edit i know swiftmailer, but I have difficulty remembering things and understanding things if i don't know the technique behind it :) So i am not stubborn to invent the wheel but want to learn how it all works ::)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.