You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP Curl如何判断HTTPS连接是首次建立还是复用?

How to Detect if a PHP cURL Connection is a New TLS Handshake or Reused (No Wireshark Needed)

Question

I want to determine whether an HTTPS connection is being established for the first time or being reused. Here's my code example:

$curlHandle = curl_init(); //first connection
$url = "https://en.wikipedia.org/wiki/Main_Page";
curl_setopt($curlHandle, CURLOPT_URL, $url);
//Reused the handle
$url = "https://en.wikipedia.org/wiki/Curl";
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_close($curlHandle);//finally closed

Is there a PHP cURL option to achieve this without using Wireshark?


Answer

Absolutely! You don’t need external tools like Wireshark—PHP cURL has built-in ways to detect connection reuse. Here are the most reliable methods:

1. Compare Connection Time Metrics

When a connection is reused, the TLS handshake step is skipped, so the connection time (CURLINFO_CONNECT_TIME) will be drastically lower (often near 0) compared to a first-time connection. You can use this to distinguish between the two:

$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);

// First request (new connection)
curl_setopt($curlHandle, CURLOPT_URL, "https://en.wikipedia.org/wiki/Main_Page");
curl_exec($curlHandle);
$firstConnectTime = curl_getinfo($curlHandle, CURLINFO_CONNECT_TIME);
echo "First request connect time: {$firstConnectTime}s\n";

// Second request (reusing handle)
curl_setopt($curlHandle, CURLOPT_URL, "https://en.wikipedia.org/wiki/Curl");
curl_exec($curlHandle);
$secondConnectTime = curl_getinfo($curlHandle, CURLINFO_CONNECT_TIME);
echo "Second request connect time: {$secondConnectTime}s\n";

// Determine reuse (adjust threshold based on your environment)
if ($secondConnectTime < 0.1) {
    echo "✅ Connection was reused!\n";
} else {
    echo "❌ New connection was established.\n";
}

curl_close($curlHandle);

2. Parse Verbose Debug Logs

Enable cURL’s verbose mode to capture detailed connection logs, which explicitly state when a connection is reused. You can redirect this output to a temporary stream instead of printing it to the console:

// Capture verbose output to a temp stream
$verboseStream = fopen('php://temp', 'w+');

$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_VERBOSE, true);
curl_setopt($curlHandle, CURLOPT_STDERR, $verboseStream);

// First request
curl_setopt($curlHandle, CURLOPT_URL, "https://en.wikipedia.org/wiki/Main_Page");
curl_exec($curlHandle);

// Second request
curl_setopt($curlHandle, CURLOPT_URL, "https://en.wikipedia.org/wiki/Curl");
curl_exec($curlHandle);

// Read and parse the log
rewind($verboseStream);
$logContent = stream_get_contents($verboseStream);
fclose($verboseStream);

// Check for reuse markers
if (strpos($logContent, "Re-using existing connection!") !== false) {
    echo "✅ Connection was reused for the second request.\n";
} else {
    echo "❌ New connection was established.\n";
}

curl_close($curlHandle);

3. Check Active Socket ID (Indirect Method)

When a connection is reused, the underlying socket ID remains the same. You can retrieve this ID with CURLINFO_ACTIVESOCKET—if it matches across requests, the connection was reused:

$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);

// First request
curl_setopt($curlHandle, CURLOPT_URL, "https://en.wikipedia.org/wiki/Main_Page");
curl_exec($curlHandle);
$firstSocket = curl_getinfo($curlHandle, CURLINFO_ACTIVESOCKET);

// Second request
curl_setopt($curlHandle, CURLOPT_URL, "https://en.wikipedia.org/wiki/Curl");
curl_exec($curlHandle);
$secondSocket = curl_getinfo($curlHandle, CURLINFO_ACTIVESOCKET);

if ($firstSocket === $secondSocket) {
    echo "✅ Connection was reused (same socket ID).\n";
} else {
    echo "❌ New connection was established (different socket ID).\n";
}

curl_close($curlHandle);

Note: The socket ID method might not be 100% reliable across all server environments, so combining it with connection time checks is a safer approach.

内容的提问来源于stack exchange,提问作者Tanguy Bernard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:49:50