蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 PHP 8.4 CLI 下 HTTP 函数可用性、URL 查询编码与 Cookie/Header 安全契约

PHPWindows x64、PHP 8.4 本地 CLI 环境。explore-ref-http.php
explore-ref-http.php89 行
<?php
declare(strict_types=1);

$cases = [];

// Case 1: Core HTTP functions availability & headers_sent()/headers_list() in CLI
$file = '';
$line = 0;
$sent = headers_sent($file, $line);
$list = headers_list();

$actual1 = [
    'sapi' => PHP_SAPI,
    'all_five_exist' => function_exists('header')
        && function_exists('headers_list')
        && function_exists('headers_sent')
        && function_exists('setcookie')
        && function_exists('setrawcookie'),
    'headers_sent_cli' => $sent,
    'headers_list_cli' => $list,
];
$expected1 = [
    'sapi' => 'cli',
    'all_five_exist' => true,
    'headers_sent_cli' => false,
    'headers_list_cli' => [],
];
$cases[] = [
    'name' => 'headers_sent_and_list_in_cli',
    'passed' => $actual1 === $expected1,
    'expected' => $expected1,
    'actual' => $actual1,
];

// Case 2: http_build_query(), parse_url(), rawurlencode()
$query = http_build_query(['q' => 'php http', 'page' => 2], '', '&', PHP_QUERY_RFC3986);
$url = 'https://www.example.com/search?' . $query;
$parsed = parse_url($url);
$raw = rawurlencode('hello world+1');
$form = urlencode('hello world+1');

$actual2 = [
    'query' => $query,
    'host' => $parsed['host'],
    'path' => $parsed['path'],
    'rawurlencode' => $raw,
    'urlencode' => $form,
];
$expected2 = [
    'query' => 'q=php%20http&page=2',
    'host' => 'www.example.com',
    'path' => '/search',
    'rawurlencode' => 'hello%20world%2B1',
    'urlencode' => 'hello+world%2B1',
];
$cases[] = [
    'name' => 'http_query_and_url_encoding',
    'passed' => $actual2 === $expected2,
    'expected' => $expected2,
    'actual' => $actual2,
];

// Case 3: Set-Cookie encoding contract (setcookie vs setrawcookie) & header CRLF guard
$cookieVal = 'user name=a+b';
$encodedHeader = 'Set-Cookie: prefs=' . urlencode($cookieVal) . '; Path=/; HttpOnly; SameSite=Lax';
$isValidRawCookie = static fn(string $v): bool => !preg_match('/[\x00-\x20,;\\\\"\r\n]/', $v);
$isSafeHeaderLine = static fn(string $h): bool => !preg_match('/[\r\n]/', $h);

$actual3 = [
    'encoded_cookie_header' => $encodedHeader,
    'raw_safe_token' => $isValidRawCookie('tok_2026_abc'),
    'raw_rejects_semicolon' => !$isValidRawCookie('a;b'),
    'header_rejects_crlf' => !$isSafeHeaderLine("Location: /ok\r\nSet-Cookie: evil=1"),
];
$expected3 = [
    'encoded_cookie_header' => 'Set-Cookie: prefs=user+name%3Da%2Bb; Path=/; HttpOnly; SameSite=Lax',
    'raw_safe_token' => true,
    'raw_rejects_semicolon' => true,
    'header_rejects_crlf' => true,
];
$cases[] = [
    'name' => 'cookie_and_header_contract_guard',
    'passed' => $actual3 === $expected3,
    'expected' => $expected3,
    'actual' => $actual3,
];

echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、PHP 8.4 本地 CLI 环境。

入口文件:projects/php-ref-http-lab/explore-ref-http.php

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-ref-http.php,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PHP

课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip

下载课程关联工程包:PHP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1293.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习