php HTTP2 PUSH

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php HTTP2 PUSH相关的知识,希望对你有一定的参考价值。

<?php
function pushAssets() {
    $pushes = array(
        "/css/styles.css" => substr(md5_file("/var/www/css/styles.css"), 0, 8),
        "/js/scripts.js" => substr(md5_file("/var/www/js/scripts.js"), 0, 8)
    );

    if (!isset($_COOKIE["h2pushes"])) {
        $pushString = buildPushString($pushes);
        header($pushString);
        setcookie("h2pushes", json_encode($pushes), 0, 2592000, "", ".myradwebsite.com", true);
    } else {
        $serializedPushes = json_encode($pushes);

        if ($serializedPushes !== $_COOKIE["h2pushes"]) {
            $oldPushes = json_decode($_COOKIE["h2pushes"], true);
            $diff = array_diff_assoc($pushes, $oldPushes);
            $pushString = buildPushString($diff);
            header($pushString);
            setcookie("h2pushes", json_encode($pushes), 0, 2592000, "", ".myradwebsite.com", true);
        }
    }
}

function buildPushString($pushes) {
    $pushString = "Link: ";

    foreach($pushes as $asset => $version) {
        $pushString .= "<" . $asset . ">; rel=preload";

        if ($asset !== end($pushes)) {
            $pushString .= ",";
        }
    }

    return $pushString;
}

// Push those assets!
pushAssets();
?>

以上是关于php HTTP2 PUSH的主要内容,如果未能解决你的问题,请参考以下文章

网站提速:Http2 & server push 实践

如何处理 http2-push 和子目录?

java9+springboot2+undertow2启用http2及server push

启用 HTTP2 服务器推送 Nginx

极客漫画:HTTP2 服务器推送

使用 .net http2 的 Apple 推送通知