1

I need to run this whole function in background, because it takes a long time to finish and so it's getting a timeout on cloudflare server. I tried more exec codes for run, but its need to get the $id with it then only it works on work.php. So i am now trying for background this whole function then may be its work.

function pancal($id) {
    for($i = 1; $i < 4; $i++) {
        _req('http://' . $_SERVER[HTTP_HOST] . '/work.php?cod=' . $id);
    }
    print 'Success';
}
andy
  • 2,002
  • 1
  • 12
  • 21
BINFAS K
  • 246
  • 3
  • 11
  • Executing your PHP program in background might still result in a timeout. What is `work.php` doing? Probably you should check whether you can improve runtime of that script. – andy Oct 14 '14 at 06:13
  • as you have not explained much what this script does but just a suggestion if your script do not required to run only in browser and can run in background then you can add it as a cron job. – Maz I Oct 14 '14 at 06:17
  • Ok, its collecting the data from the database like on facebook posts. when a visitor chose for likes. the script is continuously working when after it showing the error page. Any way to redirect the page to home page when the script start working? – BINFAS K Oct 14 '14 at 11:09

2 Answers2

0

You can use set_time_limit function to set execution time limits of your script.

Simply add set_time_limit(3600) //3600 seconds, script will not time out for one hour

if parameter is set to zero, no time limit is imposed.

You can read here more: http://php.net/manual/en/function.set-time-limit.php

Ravikumar Sharma
  • 3,678
  • 5
  • 36
  • 59
  • the script is continuously working when after it showing the error page. Any way to redirect the page to home page when the script start working? – BINFAS K Oct 14 '14 at 11:05
0
  1. Build the php script to run on the server with calls to a database.
  2. read the database for worked needed
  3. another script just to insert the work to be done into the database
  4. create a cron job to look for the job in question within the DB
  5. this will create a background job.
  6. within the script on the server build in something that will insert 1 on success or 0 on failure, into the db. including some error messages are always nice.

Hope this little bit helps you get started.

Oh, and within the script use (not recommended with 0, but you get the idea)

set_time_limit(0); //this will run forever and wont stop unless you manually go into the server and kill it.
Siniseus
  • 121
  • 6
  • the script is continuously working when after it showing the error page. Any way to redirect the page to home page when the script start working? – BINFAS K Oct 14 '14 at 11:06
  • You will most likely have to build in more logic into your scripts. My suggestion was more of a way to get started. Something like this can take some time to get working perfectly without blind issues. – Siniseus Oct 14 '14 at 23:06