Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 284857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:29:39+00:00 2026-05-12T05:29:39+00:00

I’ve got over some tutorials on how to gzip a css file in which

  • 0

I’ve got over some tutorials on how to gzip a css file in which you create a public php file to include css files with compression. The problem is I cannot get it to cache my css files. I’m using firebug for reference and I’ve actually tried using the same code to compress some javascript and it caches it fine.

Here is the code:

if(extension_loaded('zlib')){
ob_start('ob_gzhandler');
}
$offset = 60 * 60 * 24 * 31;
header('Content-type: text/css');
header ('Cache-Control: max-age=' . $offset . ', must-revalidate');
header ('Expires: ' . gmdate ("D, d M Y H:i:s", time() + $offset) . ' GMT');
ob_start("compress");
function compress($buffer) {
    // Remove Comments, White Space, End ;'s
    $buffer = preg_replace('#/\*.*?\*/#s', '', $buffer);
    $buffer = preg_replace('/\s*([{}|:;,])\s+/', '$1', $buffer);
    $buffer = preg_replace('/\s\s+(.*)/', '$1', $buffer);
    $buffer = str_replace(';}', '}', $buffer);
    $buffer = str_replace(' {', '{', $buffer);
    return $buffer;
    }

    include('global.css');

    if(extension_loaded('zlib')){
    ob_end_flush();
}

Then I’m simply referencing my php file as a css document on other pages.
As you can see I’ve tried adding max age to the mix which also proves unsuccessful.

Here are the response headers

Date    
Tue, 21 Jul 2009 19:59:19 GMT

Server  
Apache/1.3.41 (Darwin) PHP/4.4.9

X-Powered-By    
PHP/4.4.9

Cache-Control   
max-age=2592000, must-revalidate

Expires 
Thu, 20 Aug 2009 19:59:19 GMT

Content-Encoding    
gzip

Vary    
Accept-Encoding

Keep-Alive  
timeout=15, max=93

Connection  
Keep-Alive

Transfer-Encoding   
chunked

Content-Type    
text/css

Is there anything I’m missing, or a better way to go about doing this?

Thanks,

EDIT:

A script that detects whether or not the file has been changed & sending a 304 if it hasn’t, in combination with proper headers has solved this problem.

Arthur

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:29:39+00:00Added an answer on May 12, 2026 at 5:29 am

    Try adding this to the set of headers:-

    $offset = 60 * 60 * 24;
    header('Content-type: text/css');
    header('Cache-Control: max-age=' . $offset);
    header('Expires: ' . gmdate ("D, d M Y H:i:s", time() + $offset) . ' GMT');
    header('Last-Modified: ' . gmdate ("D, d M Y H:i:s", time()) . ' GMT');
    

    The problem was the must-revalidate instruction in the cache control header. This would cause the client to re-request the css each time it is needed and your code has no handling for the If-Modified-Since header and the sending of a 304 Unmodified response status code.

    The above approach reduces the cache period to 1 day and eliminates the must-revalidate instruction. It also adds the Last-Modified header (a cache may choose not to cache an item when a Last-Modified or ETag header is missing).

    To improve this you could find the actual last modified time of the css file you are compressing and send that as the Last-Modified header. You could include in your code a comparison of the requests If-Modified-Since header with the value of the css files last modified time. If you find them the same send this set of headers but also send a 304 Unmodfied status and don’t send the body at all. (I’m not really a PHP person so I’ll leave that to the PHP experts to place in another answer).

    Make a realistic assessment of how long you would want a client cache the css before it must make another attempt to retrieve it and set the max-age value accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 160k
  • Answers 160k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm willing to be proven wrong, but test frameworks in… May 12, 2026 at 11:39 am
  • Editorial Team
    Editorial Team added an answer Phil Haack, one of the creators of ASP.Net MVC, put… May 12, 2026 at 11:39 am
  • Editorial Team
    Editorial Team added an answer I'd suggest not viewing this as something you have to… May 12, 2026 at 11:39 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.