Jump to content

stop php execution and print results (NOT FATAL ERROR)


robcrozier

Recommended Posts

Hi everyone.

 

What i'm trying to do is stop my php file from executing after a certain amount of time (say 30 seconds).  This is because the file will take a long time to fully execute.  However, i want it to actually print something on the screen after it stops (i.e the information it has processed thus far).  I do not want it to display a fatal error just like it would if i changed the php max execution time setting.

 

Any ideas?

 

Cheers!

Link to comment
Share on other sites

you could start a process wich chekcs the time and start another process wich does your script, in the process that does your script you can break out of that process and jump into the timmer process which checks the time if the time is ok that process breaks out and jumps back into your script process wich continues from where it jumped out then furtehr down the script yoiu can jump back out into the timer process and check the time again and if the time is too much you can return or pipe throgh a flag from the timer process so that when it jumps back into the script process it has a flag which tells the script process to die.

 

http://uk3.php.net/manual/en/function.pcntl-fork.php

Link to comment
Share on other sites

Or even output buffering with a timeout handler:

 

register_shutdown_function("say_goodbye");

function say_goodbye() {
   if (connection_aborted()) {
      //  user has aborted the request, so perhaps redirect back to previous page
   } elseif (connection_status() == CONNECTION_TIMEOUT) {
      //  PHP has hit timeout. Flush the output buffer
   } else {
      //  Normal termination, so do nothing more
   }
}

Link to comment
Share on other sites

you could start a process wich chekcs the time and start another process wich does your script, in the process that does your script you can break out of that process and jump into the timmer process which checks the time if the time is ok that process breaks out and jumps back into your script process wich continues from where it jumped out then furtehr down the script yoiu can jump back out into the timer process and check the time again and if the time is too much you can return or pipe throgh a flag from the timer process so that when it jumps back into the script process it has a flag which tells the script process to die.

 

http://uk3.php.net/manual/en/function.pcntl-fork.php

 

jajaja im dum

 

 

you can just do a function wich checks the time and if teh time is ove rteh limit then just die and echo data

Link to comment
Share on other sites

you can just do a function wich checks the time and if teh time is ove rteh limit then just die and echo data

Why bother?

 

PHP will cease executing at it's predefined timeout anyway. Why not let PHP terminate as normal, but trap the timeout error and replace it with your own output.

Link to comment
Share on other sites

you can just do a function wich checks the time and if teh time is ove rteh limit then just die and echo data

Why bother?

 

PHP will cease executing at it's predefined timeout anyway. Why not let PHP terminate as normal, but trap the timeout error and replace it with your own output.

 

will that be using teh ini set function becaus eyou dont whant to set it for the whole server ??

Link to comment
Share on other sites

will that be using teh ini set function becaus eyou dont whant to set it for the whole server ??

The php.ini setting is for the whole server, though it can be overridden by the web server timeout setting in httpd.conf (for apache), but you can over-ride it within your individual scripts using set_time_limit() or ini_set().

However it is set, when PHP reaches that time limit the script will terminate with a timeout.

 

register_shutdown_function() allows you to define a block of code that will be executed whenever and however the script terminates. That can be a timeout, a user abort, a normal termination (through exit() or die() or simply the end of the script). You can then determine how the termination has been triggered, as shown in my example, and run a small block of code before ending execution.

I use it to trap for timeouts in Ajax calls and return a status code to the browser, which can then take appropriate action (display a special message to the user offering them alternative functions such as re-submitting their request to run as a background task rather than interactively).

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.