Skip to content
gtbu edited this page Oct 10, 2025 · 5 revisions

There are ways to use HTTP/2 features in PHP, even though the header() function was primarily developed for HTTP/1.1. However, HTTP/2 support is largely handled by the web server (e.g., Apache, Nginx) and not directly controlled at a low level in PHP.

For specialized HTTP/2 features like Server Push, you don't need a separate library. Instead, you can use the existing header() function to set the Link header. The web server interprets this header and initiates the push.

HTTP/2 Server Push with PHP

To send resources such as CSS or JavaScript files via Server Push, you can use the Link header with the rel=preload attribute.

// CSS-Datei per Push senden
header("Link: </css/styles.css>; rel=preload; as=style", false);

// JavaScript-Datei per Push senden
header("Link: </js/main.js>; rel=preload; as=script", false);
?>

// Image-Datei per Push senden
header("Link: </images/site/logo--red.svg>; rel=preload; as=image", false);

<!DOCTYPE html>
<html>
<head>
    <title>HTTP/2 Server Push Beispiel</title>
    <link rel="stylesheet" href="/css/styles.css">
</head>
<body>
    <h1>Hallo Welt!</h1>
    <script src="/js/main.js"></script>
</body>
</html>

The third parameter, false, in the header() function ensures that multiple link headers can be sent without overwriting each other.

The as attribute specifies the type of resource being sent (e.g., style for CSS, script for JavaScript, image for images).

Your web server must be configured for HTTP/2 for this to work.

Push Api

Web push is a technology that enables web applications to deliver messages to a user's device, even when they aren't actively using the application. This is especially useful for delivering real-time updates, alerts, or other important information in a timely manner.

Clone this wiki locally