Forum Moderators: coopster

Message Too Old, No Replies

Redirecting during a script process

Showing a new page while the script is running

         

lasko

2:27 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



I am vey new to Php and I have made my own script to allow a user to post a holiday rental enquiry to over 80 properties by filling in one form.

The form finds all the email addresses in a MYsql database
and sends it two anyone who has a two bedroom apartment.

Works find however because it takes a long time to process nearly 1 minute the browser times out and shows page not found.

I am using

Header("Location: $fredirect");

to redirect the user to a success page.

How can I solve this problem?

the header("location: $fredirect"); is at the bottom of the script as the last thing it reads should I move this to the top before the email command or will it cause a conflict where by the emails wont be sent because a new page was displayed

Any adice would be grateful

daisho

2:44 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



That's tricky since even though you do the redirect the browser will not redirect until the webserver tells them the process is finished.

In situations like this I've always setup some type of daemon process. On the form you simply insert the request into the database is a "pending" table.

From there the easiest thing to do is setup a cron script to fire every 5 or 10 min to process "pending" requests. This way user doesn't have to wait and no browser timeouts. There is a slight lag in the emails going out but nobody will notice I'm sure.

daisho.

lasko

2:48 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



So what i would do is post the form into a table and have a automatic script running on the server that will access this table and send anything thats in it.

How on earth am I going to do this?

brotherhood of LAN

2:53 pm on Jun 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



//deleted nm ;)

lasko

2:56 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



sorry brother of lan slightly confused with your post

jatar_k

4:49 pm on Jun 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I sometimes just write it to a logfile (textfile) and then have a parser that runs on a cron every night to read and process the information.

Every night might be too long in this case but you could decide on a logical interval maybe every hour.

You could also use the syslog but I doubt you would have access to that unless you run your own server. The thing is to make it fire and forget or the quickest return possible. All the other processing can be done afterward by your parser.

jaski

5:26 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



Even if worked that way lasko .. I see another problem with the way you are doing it .. What will happen if user clicks refresh .. will it send emails twice? ..

Sending emails would be best left to another script as daisho and jatar_k have suggested.

eg. your current script keeps adding records about whom to send email .. and another script runs every minute/hour/day from cron to see what jobs are there to do .. and does them.

Also if you want .. you can setup data tables in a way that avoids duplicate emails getting sent (because of refresh by users)...by using a unique key appropriately.

vincevincevince

5:40 pm on Jun 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i have three suggestions

1 - make the script recursive, i.e. it sends one email, then calls the same script again, whic sends the second email, calls the script again, etc... until finished - when it redirects. this could be done with passing a counter
pseudocode: send email to address $a, redirect to $PHPSELF?emailid=$a++, unless $a=final email... in which case redirect to success

2 - edit your php.ini file to increase the execution time.


;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;
max_execution_time = 3000 ; Maximum execution time of each script, in seconds

3 - include a number of email addresses in the bcc section of the email and then let the smtp server split it up and send it out

lasko

6:08 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



The bcc sounds like the solution.

However it would stop me writing adding personal information into the emails. For example

The script extracts the row of an owner and writes into the subject field their ref: number, url and so on.

But the bcc idea seems to be the solution I will have to make a universal email and make it send to all the owners with just the content of the form.

Thanks for the help and ideas.

daisho

7:25 pm on Jun 28, 2003 (gmt 0)

10+ Year Member



The way my suggestion would work would be to create a table like:

tblLeads:
id ¦ name ¦ email ¦ notes ¦ etc ¦ etc

The table schema would be the same as all the fields that you are posting with the form. Basically save all the form fields to a row in the database.

Then every 10min or so in a cron do something like:

  1. select * from tblLeads
  2. Loop for each item:
    a. Do your matching and send out emails
    b. delete this row from tblLeads

Sander

12:18 am on Jul 2, 2003 (gmt 0)

10+ Year Member



I don't know if I understood the question fully,
but is your problem that you can't redirect at the end of a page because of headers already being sent?

In that case, check out ob_start();