Jump to content
kixe

Wrong header sent if template caching enabled

Recommended Posts

Hi there,

I am using the following template file to provide a pdf download, of a pdf stored in a file-field.

<?php
$file = $page->pdf->filename;
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);

If template caching is disabled, everything working as expected and the following header is sent

HTTP/1.1 200 OK
Date: Fri, 26 Feb 2016 13:03:27 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Disposition: attachment; filename="anyfile.pdf"
Content-Length: 909233
X-XSS-Protection: 1; mode=block
Keep-Alive: timeout=2, max=1000
Connection: Keep-Alive
Content-Type: application/pdf

Using Template cache the header is overwritten by

HTTP/1.1 200 OK
Date: Fri, 26 Feb 2016 13:06:11 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-XSS-Protection: 1; mode=block
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=2, max=1000
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

Any ideas to prevent this?
 

Share this post


Link to post
Share on other sites

If you're using a dedicated template (not urlSegments) you can set the content type in the template's settings. 

Share this post


Link to post
Share on other sites

@LostKobrakai
Thanks, I know this. But this is only the Content-Type header. I want to change some others too! As a minimum: Content-Disposition and Content-Length.

Share this post


Link to post
Share on other sites

That's probably not possible without hooking somewhere in the cache. The template cache does probably only store the content, but not any headers, which can be desired or not desired.

Share this post


Link to post
Share on other sites

It works using wireSendFile(). New template:

<?php
/**
 * using this template allows you to use template cache
 *
 */
$file = $page->pdf->filename;
$disposition = 'inline'; // change to 'attachment' to provide download
$headers = array(    
    "content-length" => filesize($file),
    "content-disposition" => 'inline; filename="'.basename($file).'"'
);
wireSendFile($file, array('forceDownload' => false), $headers);

Define any headers to set new or overwrite default. "Force download 'must be set to false to avoid that 'Content-Disposition' is overwritten due to file extension.

But I am not sure if this uses the cache. Maybe its useless anyway it takes the same time to render.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mithlesh
      Changing it to null
    • By Mithlesh
      Hi, I have one URL - writerrelocations.com/contact-now/

      I have one issue where my header Image is appearing again after the contact form, for your reference:



      I have checked my processwire template section where have not added any Image field besides Header Image, attaching the same for the reference:




      PLEASE help me to get rid of the image below contact form

      Waiting for the swift response!

      Thanks
       
    • By Liam88
      Hi all,
      Quick question as I haven't found anything from my Googling. 
      I have a blog on the site which utilises pagination. On the standard blog page I have a custom header which has featured posts.
      Below that I then have recent posts like mosts blogs.
      Now if i click to page two or three I want the header to disappear and just show a continuation of the standard posts.
      I'm not sure how to go about this so any direction would be helpful.
      Thanks
    • By NorbertH
      As we are building some webapplications with the PW backend , having those really big spacings between all field elements in forms and everywhere is not too practical. 
      UI Kit already has less spacings between all elements than Bootstrap for example , but i am looking fore a more condensed BE Theme.  Maybe someone already made something like that?
×
×
  • Create New...