PHP stream_socket_enable_crypto() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

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.

PHP
$ok = stream_socket_enable_crypto($stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
CallWhat 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
PHP
stream_socket_enable_crypto(resource $stream, bool $enable, ?int $crypto_method = null, ?resource $session_stream = null): int|bool

Parameters

ParameterDescription
streamRequired. A connected stream that should start or stop encryption.
enableRequired. true to enable crypto, false to disable.
crypto_methodOptional. A STREAM_CRYPTO_METHOD_* constant. Required when enabling.
session_streamOptional. Another stream to reuse an existing SSL session from.

Return Value

Return ValueDescription
trueThe crypto state changed successfully.
falseThe handshake or change failed.
0More data is needed (typical for non-blocking streams).

PHP Examples

Example 1 — Crypto Enable Call Shape

Document the usual TLS client enable call with fixed tutorial output.

PHP
<?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;

How It Works

  1. Connect with plain TCP first for STARTTLS-style protocols.
  2. Then call stream_socket_enable_crypto() with true.
  3. Choose a STREAM_CRYPTO_METHOD_* client or server constant.
  4. true means the handshake finished.

Example 2 — Safe SSL Context Options

Build a context that keeps peer verification on before any TLS upgrade.

PHP
<?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;

How It Works

  1. Create the context before connecting or enabling crypto.
  2. verify_peer checks the certificate.
  3. verify_peer_name checks the host name.
  4. Leave both on for production.

Example 3 — Offline Pair Placeholder

Use a socket pair offline and print that crypto needs a real TLS peer.

PHP
<?php
$pair = stream_socket_pair(STREAM_PF_INET, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
[$a, $b] = $pair;
echo 'pair ready for plain data', PHP_EOL;
echo 'enable_crypto needs a real TLS peer', PHP_EOL;
fclose($a);
fclose($b);

How It Works

  1. stream_socket_pair() works offline for plain bytes.
  2. A TLS handshake needs certificates and a real peer.
  3. Use the pair for send/recv practice without crypto.
  4. Enable crypto only against a proper TLS endpoint.

Frequently Asked Questions

It turns encryption on or off for a connected stream, often after a plain TCP connect for STARTTLS.
On non-blocking streams, 0 means the handshake needs more data. Try again when the stream is readable or writable.
Prefer modern STREAM_CRYPTO_METHOD_TLS_CLIENT or STREAM_CRYPTO_METHOD_TLS_SERVER constants for TLS.
Only on a controlled local lab if you must. Never disable verification for production traffic.
After the stream is connected and, for STARTTLS, after the server says encryption may begin.

Next: stream_socket_get_name()

stream_socket_get_name() returns the local or remote address name of a socket stream.

stream_socket_get_name() 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