Home PHP Stream stream_socket_enable_crypto() PHP stream_socket_enable_crypto() Function Definition In PHP, stream_socket_enable_crypto() turns encryption on or off for an already connected stream.
It is used for STARTTLS-style upgrades after a plain TCP connect. Pass true to enable and a STREAM_CRYPTO_METHOD_* constant. The result is true on success, false on failure, or 0 when more data is needed for a non-blocking handshake.
Syntax Call stream_socket_enable_crypto() with the values you need. Start with this line.
$ok = stream_socket_enable_crypto($stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT); Call What it does stream_socket_enable_crypto($stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)Starts a TLS client handshake on the stream. stream_socket_enable_crypto($stream, false)Turns encryption off when the protocol allows it. stream_socket_enable_crypto($stream, true, STREAM_CRYPTO_METHOD_TLS_SERVER)Starts a TLS server-side handshake.
You normally connect with stream_socket_client() first, then enable crypto for protocols that upgrade after greeting. Pair with a stream context that keeps verify_peer true. Offline pair demos show the call shape without a real handshake.
Library declarations stream_socket_enable_crypto(resource $stream, bool $enable, ?int $crypto_method = null, ?resource $session_stream = null): int|bool Parameters Parameter Description stream Required. A connected stream that should start or stop encryption. enable Required. true to enable crypto, false to disable. crypto_method Optional. A STREAM_CRYPTO_METHOD_* constant. Required when enabling. session_stream Optional. Another stream to reuse an existing SSL session from.
Return Value Return Value Description true The crypto state changed successfully. false The handshake or change failed. 0 More data is needed (typical for non-blocking streams).
PHP Examples 1. Crypto Enable Call Shape 2. Safe SSL Context Options 3. Offline Pair Placeholder Example 1 — Crypto Enable Call Shape Document the usual TLS client enable call with fixed tutorial output.
<?php
// After stream_socket_client('tcp://mail.example:587') and a STARTTLS reply:
// stream_socket_enable_crypto($stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
echo 'crypto method: STREAM_CRYPTO_METHOD_TLS_CLIENT', PHP_EOL;
echo 'on success: true', PHP_EOL; crypto method: STREAM_CRYPTO_METHOD_TLS_CLIENT
on success: true How It Works Connect with plain TCP first for STARTTLS-style protocols. Then call stream_socket_enable_crypto() with true. Choose a STREAM_CRYPTO_METHOD_* client or server constant. true means the handshake finished. Example 2 — Safe SSL Context Options Build a context that keeps peer verification on before any TLS upgrade.
<?php
$ctx = stream_context_create([
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
],
]);
$opts = stream_context_get_options($ctx)['ssl'];
echo 'verify_peer=', $opts['verify_peer'] ? 'on' : 'off', PHP_EOL;
echo 'verify_peer_name=', $opts['verify_peer_name'] ? 'on' : 'off', PHP_EOL; verify_peer=on
verify_peer_name=on How It Works Create the context before connecting or enabling crypto. verify_peer checks the certificate. verify_peer_name checks the host name. Leave both on for production. Frequently Asked Questions What does stream_socket_enable_crypto() do in PHP? It turns encryption on or off for a connected stream, often after a plain TCP connect for STARTTLS.
What does a return of 0 mean? On non-blocking streams, 0 means the handshake needs more data. Try again when the stream is readable or writable.
Which crypto method should I use? Prefer modern STREAM_CRYPTO_METHOD_TLS_CLIENT or STREAM_CRYPTO_METHOD_TLS_SERVER constants for TLS.
Should I disable verify_peer to test? Only on a controlled local lab if you must. Never disable verification for production traffic.
When do I call it? After the stream is connected and, for STARTTLS, after the server says encryption may begin.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion