PHP socket_set_timeout() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, socket_set_timeout() sets how long a stream will wait during a read or write before giving up.

It is an alias of stream_set_timeout(). Pass whole seconds and optional microseconds. After a timeout, reads may return false or incomplete data, and stream metadata marks timed_out as true. Use it on network sockets so a silent remote host cannot hang your script forever.

Syntax

Call socket_set_timeout() with the values you need. Start with this line.

PHP
socket_set_timeout($stream, 5, 0);
CallWhat it does
socket_set_timeout($stream, 10)Waits up to 10 seconds for stream operations.
socket_set_timeout($stream, 0, 500000)Waits up to half a second using microseconds.
stream_set_timeout($stream, 2, 0)Same timeout using the stream_* name.

Teach both socket_set_timeout() and stream_set_timeout(). The timeout is seconds + microseconds. Check the return value, and after a slow read inspect timed_out through socket_get_status() / stream_get_meta_data().

Library declarations
PHP
socket_set_timeout(resource $stream, int $seconds, int $microseconds = 0): bool

Parameters

ParameterDescription
streamRequired. An open stream resource, often from fsockopen() or pfsockopen().
secondsRequired. Whole seconds to wait.
microsecondsOptional. Extra microseconds to wait. Defaults to 0.

Return Value

Return ValueDescription
trueThe timeout was applied.
falseThe timeout could not be set.

PHP Examples

Example 1 — Set a Five-Second Timeout

Apply a timeout on a php://temp stream and print the boolean result.

PHP
<?php
$stream = fopen('php://temp', 'r+');
$ok = socket_set_timeout($stream, 5, 0);
var_dump($ok);
fclose($stream);

How It Works

  1. fopen('php://temp') gives a safe offline stream.
  2. socket_set_timeout() is an alias of stream_set_timeout().
  3. 5 seconds and 0 microseconds become the wait limit.
  4. true means PHP accepted the setting.

Example 2 — Use Seconds and Microseconds Together

Set a short timeout of 1.5 seconds and confirm success with both alias names in comments.

PHP
<?php
$stream = fopen('php://temp', 'r+');

// Same as stream_set_timeout($stream, 1, 500000)
$ok = socket_set_timeout($stream, 1, 500000);
echo $ok ? 'timeout set to 1.5s' : 'failed', PHP_EOL;

fclose($stream);

How It Works

  1. 1 second plus 500000 microseconds equals 1. 5 seconds.
  2. Microseconds let you choose sub-second waits.
  3. The socket_* name works like the stream_* name.
  4. Always keep timeouts short enough for your UI or job limits.

Example 3 — Check timed_out Metadata

Set a timeout, then read the timed_out flag from metadata before any slow network read happens.

PHP
<?php
$stream = fopen('php://temp', 'r+');
stream_set_timeout($stream, 2, 0);

$meta = socket_get_status($stream);
echo 'timed_out before read: ', $meta['timed_out'] ? 'yes' : 'no', PHP_EOL;
echo 'timeout ready for network streams', PHP_EOL;

fclose($stream);

How It Works

  1. stream_set_timeout() stores the limit on the stream.
  2. timed_out stays false until an operation actually times out.
  3. socket_get_status() reads that flag (alias of stream_get_meta_data()).
  4. On real sockets, check timed_out after a failed or partial read.

Frequently Asked Questions

It sets the read/write timeout on a stream. It is an alias of stream_set_timeout().
The wait limit is seconds plus microseconds. For example 1 and 500000 means one and a half seconds.
No. It only stops waiting. You should check the operation result and the timed_out metadata key.
Remote servers can stall. A timeout keeps your PHP script from hanging forever on fgets() or fread().
No. They are aliases. Learn both names so older and newer examples make sense.

Next: syslog()

syslog() writes a message to the system logger using a priority such as LOG_INFO or LOG_ER.

syslog() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful