4

I am building my blog by PHP and Godaddy apache server, recently I face a problem that I cannot clear the explorer cache so that each time when I change my style.css I have to change the name of the css file. So can you tell what is wrong or how can I clear the css cache?

1

8 Answers 8

10

You can make the browser automatically clear cache if you create a version based on file modified time

eg:

$filename = '/css/style.css';
$fileModified = substr(md5(filemtime($filename)), 0, 6);
// $fileModified = filemtime($filename); // - with version as timestamp.

and HTML

<link rel="stylesheet" type="text/css" href="<?php echo $filename;?>?v=<?php echo $fileModified ; ?>">
Sign up to request clarification or add additional context in comments.

6 Comments

Why to md5 if you can just use filemtime?
Because I believe is fancier, why everyone needs to see when you modified your files? :)
nothing wrong with having filemtime ;-) Personally I prefer not to do things, that bring no benefits. That is why I asked about why to md5. I've upvoted you because your answer is the most correct among others, but another unnecessary step with md5 still confuses me
Mihai, thank you so much for your work. Here you give me a bright new idea to do something similar, but Mina's answer seems work for me, so still upvoted yours. Cheers.
@BGMEMO yes, but thing it will solve only in your browser. Others may have to wait for browser to clear cache.
|
5

Put Version Number for your css file like following,

style.css?v=1 or style.css?v=1.1

Comments

2

Clear the Browsing History cache in your Browser... This is not related to PHP..

5 Comments

actually you could use php to append a timestamp to your CSS reference.
well clearing his browser history doesnt clears his users' browsing history
Yes I understand that, but I mean the whole functionality and how the browser treats a CSS file is not related to PHP.
Thank you Mina, it works for me. It is really not related to PHP. I have thought it is the problem of setting css expiredate in htaccess, but it is the problem of my brwsing history.
Please share more details. How would this work for other visitors of a website? Should the OP add a warning to their site, that the cache should be cleared on each request?
2

Is this what you asking for?

<link href="style.css?key=<?php echo time(); ?>" type="text/css" rel="stylesheet" />

Best is to keep it like this on your local or testing server only - remove time() part when the site goes live

UPDATE

or instead of time() you could use key=<?php echo date('h'); ?> for hourly or change as you'd like it be.

24 Comments

this could effect the performance of you site as the css will never be cached.
@HappyApe: I just wanted to say that this is the terrible way to solve OP's problem
@HappyApe: it's not about "like/not like" it's about efficiency. Your solution less efficient than the most upvoted one. It is just a fact. And it makes no sense arguing more - but your solution is just bad, because it prevents browsers of using cache.
@HappyApe: it makes sense to solve any issue using the best possible solution. Random filenames is just a bad way of serving static files.
@HappyApe: so in your projects you ask your users to change their browsers? I prefer to implement solutions that work regardless of browser used.
|
1
<link href="style.css?t=[timestamp]" type="text/css" rel="stylesheet">

3 Comments

it will force a get of the latest file
@zerkms no it treats it as a totaly different file,
@Blowsie: I know that. But do you realize that will impact the overall page loading time? Nowadays people try to make their sites faster by using caching and reducing amount of HTTP requests
0

Use a random number as a parameter with your CSS file. It is one of the easiest way to cache everytime on your browser when file get loaded.

Ex:

style.css?v=12345

4 Comments

Random number each request?
@zerkms.. yes each and every time we need the randon number for browser loading because the above requirement is for frequent changes. But we have to remove this additional parameter after certain time for improving the server performance.
well "every time we need the randon number for browser loading because the above requirement is for frequent changes" --- that's not true. There are solutions without any randomness. See the most upvoted answer
Please add some more explanation to your answer such that others can learn from it. Especially if you had to remove that parameter again: what's the alternative?
-1

For chrome: click on the top right menu button, click on settings, scroll down to advanced and click on it, and click on "Clear browsing data" and tick Cached images and files.

1 Comment

Please share more details. How would this work for other visitors of a website? Should the OP add a warning to their site, that the cache should be cleared on each request?
-2

Just press Ctrl+F5 in browser or another combination that update page with clearing cache.

1 Comment

Please share more details. How would this work for other visitors of a website? Should the OP add a warning to their site, that the cache should be cleared on each request?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.