PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
The reason I want to do this is because some of the WHOIS servers out there
tend to be down from time to time and I would like to set a timeout of
around 30 seconds. and when those 30 seconds have passed I'd like to output
that a timeout has occurred. Can anyone tell me how to achive this?
"Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error."
The problem with this is that it returns a fatal error, I would like to output a message to the user instead. I don't know how. Another thing I should add is that the code above is running in a for loop and if for example the timeout is in the 6th loop I would still like to be able to execute the 7th and 8th loop or howmany other loops are left...
I read about error handling in the manual and thought I might get it to work with set_time_limit(). But I saw the following:
Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), the sleep() function, database queries, etc. is not included when determining the maximum time that the script has been running.
So if system() isn't influenced by this, then my exec() functions also isn't affected by setting a timelimit, since my whois query happens outside the script itself....
Can someone please shed some light on this as to how I can set a timeout on a whois query in an exec()?
sleep() delays program execution and I don't see how that helps me terminate an exec() after a given number of seconds. But maybe I am wrong..... can you tell me how this would help me?
It looks like sleep doesn't work after exec() unless I am doing something wrong. There has to be a good way to put some sort of maximal execution time on an exec()................
Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), the sleep() function, database queries, etc. is not included when determining the maximum time that the script has been running.
Meaning that external execution of programs is not affected by set_time_limit()
You can't do that in PHP. PHP is a single thread language. The only function I know of that allows a timeout on a stream is stream_set_timeout() which works in combination with fsockopen() or any other stream input.Seeing as your function is not a stream it looks like you are stuck.
Could you fork the process to allow the main script to run while waiting for a result? Then you could use sleep on the main process and check each time for a result...if nothing occurs then end the sleep / check cycle and continue...
lostboy wrote:Could you fork the process to allow the main script to run while waiting for a result? Then you could use sleep on the main process and check each time for a result...if nothing occurs then end the sleep / check cycle and continue...
Of course you can't. Read my post. PHP is a single thread language.