Skip to content

Instantly share code, notes, and snippets.

@jgrahamc
Last active June 18, 2020 09:38
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgrahamc/df91229381366105c7ff0f88c8c38485 to your computer and use it in GitHub Desktop.
Save jgrahamc/df91229381366105c7ff0f88c8c38485 to your computer and use it in GitHub Desktop.
Demonstrating how to use Link headers to make CloudFlare push assets
<?php
function ccbysa($imagehtml, $sourceuri, $owneruri, $ownername) {
return <<<HTML
{$imagehtml}
<br/>
<small><a href="https://creativecommons.org/licenses/by-sa/2.0/">CC BY-SA</a>
<a href="{$sourceuri}">image</a> by <a href="{$owneruri}">{$ownername}</a>
<br/>
HTML;
}
function ccbynd($imagehtml, $sourceuri, $owneruri, $ownername) {
return <<<HTML
{$imagehtml}
<br/>
<small><a href="https://creativecommons.org/licenses/by-nd/2.0/">CC BY-ND</a>
<a href="{$sourceuri}">image</a> by <a href="{$owneruri}">{$ownername}</a>
<br/>
HTML;
}
function pushImage($uri) {
header("Link: <{$uri}>; rel=preload; as=image", false);
return <<<HTML
<img src="{$uri}">
HTML;
}
$image1 = pushImage("/images/drucken.jpg");
$image2 = pushImage("/images/empire.jpg");
?>
<html>
<head><title>PHP Server Push</title></head>
<body>
<h1>PHP Server Push</h1>
<?php
echo ccbysa($image1, "https://bit.ly/1Wu5bYx",
"https://www.flickr.com/photos/hiperactivo/", "Javier Candeira");
echo ccbynd($image2, "https://bit.ly/24PHue3",
"https://www.flickr.com/photos/bobsfever/", "Robert McGoldrick");
?>
</body>
</html>
@holtkamp
Copy link

Nice post on https://blog.cloudflare.com/using-http-2-server-push-with-php/, but is this example hosted somewhere so we can have a look of the result? Could not find the link in the blog post...

@gbowman2013
Copy link

gbowman2013 commented Nov 18, 2016

See https://blog.cloudflare.com/using-http-2-server-push-with-php/ for hosted code and discussion of this topic. The code result is shown at the top of the post.

@developernaren
Copy link

This might be a stupid question. I copied and pasted the whole code of this in my local machine file but I cannot see anything in the screen. How is this supposed to work? Do I need to enable something or have some settings in my local machine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment