Php set_time_limit wont take into account socket operations

By | March 8, 2009

The PHP function set_time_limit is used to set a time limit on the maximum execution time of a script. But if the script has socket operations using fsockopen , fread and fwrite or even CURL then the set_time_limit may not appear to have any effect on the scripts timelimit or timeout.

Again if safe mode is on then also set_time_limit has no effect. (Safe Mode)

Socket operations are stream operations which do not count in the script execution time. says :

Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running.

Therefore socket operations are beyond the php scripts execution time and if they get stuck then the script can run for a long time unless terminated externally by some resource-usage checking mechanism.

The 5th parameter of fsockopen is the timeout value to be used for controlling the amount of time that is spend on trying to connect to a host. For socket read and write operations via fsockopen stream_set_timeout is the function which should be used to put a time limit on the socket trying to read on a stream. socket_set_timeout is an alias of this function.

Function stream_set_timeout may appear to have no effect if there is a write operation in between the stream_set_timeout and the read operation. For e.g. fsockopen , stream_set_timeout , fwrite then fread. So it must be used directly before any read operation to have effect on the read time.


In case of CURL curl_setopt should be used to set CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT to relevant values. The first one controls the maximum amount of time a socket spends on trying to connect to a host and the second one controls the maximum amount of time a curl operation can execute for.

php.ini settings

The default_socket_timeout in php.ini which can be set like ini_set('default_socket_timeout', 2);
can be used to set the default socket timeout i.e. the 5th parameter of fsockopen.

So for php scripts that have socket operations set_time_limit may not appear to put an effective time limit on execution time. So the above functions and configuration can implement the same and prevent the script from hogging resources if a particular host is down and a socket spends all time in trying to connect to it or trying to read or write data to it.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

One Comment

Php set_time_limit wont take into account socket operations
  1. yegle


    When using socket_read,set to non-blocking reading by setting the third parameter to PHP_NORMAL_READ.When nothing can be read, empty string will be returned.

Leave a Reply

Your email address will not be published. Required fields are marked *