?
Solved

How to fire off PHP script/page in background?

Posted on 2005-02-25
9
Medium Priority
?
157 Views
Last Modified: 2006-11-17
I have a PHP script that rolls through a large table of email addresses and sends out a newsletter based on custom content drawn from the database.  On average I calculate this script to take 2 minutes or so to run.

Is there a way to fire off the process in the background so that the page does not have to wait to be loaded in the browser?  I am afraid that those who are running the mailing script will close the window or hit stop and screw things up.  Can I somehow run it in the background or are there other ideas?  Thanks!

FYI, I am on a shared hosted webserver, so I don't have root access to anything.
0
Comment
Question by:djs120
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 4

Expert Comment

by:Kooroo
ID: 13408644
a couple of options. but I have to ask, this will be a user initiated request to fire off these emails?

One way to do it is via a trigger somewhere.
-Essentially, when a user visits your page and submits a request to fire off the newsletter, all your script does is create a 0-byte file in your home directory.
-You have a cron job running under your username(most shared hosting have control panels to create cron/scheduled jobs) that runs every minute. All this job does is this:
if trigger file exists, call the php script via a curl or just a shell execute since it won't return anything, then erase the trigger file.

so now what you've done is taken away the long running process from your end user and turned it into a trivially short one (touch a file). and the cron job should also be trivially small until it senses that you actually need the job run so it shouldn't be too big a deal to run it fairly frequently.
0
 
LVL 4

Expert Comment

by:Kooroo
ID: 13408646
ps. - also, this will keep multiple users from sending out your mailing list a bagillion times simultaneously and pissing off your host.
pps - don't forget to double check with your host that you are able to send out all these emails off their smtp.
0
 
LVL 1

Author Comment

by:djs120
ID: 13408681
Unfortunately my host is really crappy and I can't set up cron jobs from the control panel.  I doubt the host will set one up for me.  Is there something I can do using the PHP exec() command or system() command (or something close to that?).
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:Kooroo
ID: 13408825
what host is this? do they offer shell access? you could do it via command line if they do.

another solution is to look at the reference for the php function set_time_limit(). This isn't an absolute solution though because you'll still be bound by global settings...but it should prevent users from messing your script up with a stop.

It is my opinion that the cron job is best because it eliminates user misfunction fairly cleanly.
0
 
LVL 6

Expert Comment

by:quad341
ID: 13408881
all you need to do is ignore user abort.  depending on they type of server depends on the setting.  true for *nix, false for some versions of windows.

just add to the top of the script:
ignore_user_abort(true); // for *nix
0
 
LVL 1

Accepted Solution

by:
wasco earned 2000 total points
ID: 13410597
what i do if i have to write things like this for customers is the following

on top of the page i have the following code
session_start();
if(!isset($_SESSION['current'])) { $_SESSION['current'] = 0; }

then i query the db to get the first 10 record
"SELECT * FROM emails order by emailaddress LIMIT " .  $_SESSION['current'] . ",10"

this takes 10 email addresses of the db.
then send the emails to those people

last but not least,
 $_SESSION['current'] =  $_SESSION['current']+10;
header('location: ' . $_SERVER['PHP_SELF']);

so the script takes 10 email adresses of the list
when finished forwards to itself to do the next 10

alternatively you could create a new column in your db to keep track of what record has its email send and instead of using a session-current var, just select 10 records that havent been send
if no record are found, your script is finished and you can show a text onscreen to congrats the user.

i've once taken it a little further by showing the user the progress of the operation
at the beginning of the script i show a percentage on the screen
when the emails are send instead of using the header (which cant be used after showing something on screen) i used javascript's document.location.href to start the next batch
by doing this the user could see the progress of the email sending

0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13410694
If you can get decent access, this is the 'proper' way to keep a PHP script running all the time in the background:

http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_20977409.html

I use this mechanism for sending out mailing lists.
0
 
LVL 1

Author Comment

by:djs120
ID: 13412655
I like your idea, wasco, with showing the user progress on the task.  I will try implementing this and will return to post the solution here...
0
 
LVL 1

Author Comment

by:djs120
ID: 13425420
Wasco, I used your idea and did the % bar feature by using a META REFRESH to change the location and display how many records have been completed so far:

<META HTTP-EQUIV="Refresh" CONTENT="1; URL=<?=$_SERVER['PHP_SELF']?>">

Thanks!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question