FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to set an expiry date for images?





likeabreeze
I tested my website at Google PageSpeed Insights, and I get the results,
Leverage browser caching.
It told me to specify an expiration at least one week in the future for the images,
"Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network."
In the HTTP headers?
How to do this? Question Question
Peterssidan
You can use mod_expires.

To set the expiry date one month in the future for all file types you can use the ExpiresDefault directive like this (put it in a .htaccess file).
Code:
ExpiresActive On

ExpiresDefault "access plus 1 month"


To set the expiry date for certain file types you can use the ExpiresByType directive.
Code:
ExpiresActive On

ExpiresByType image/png "access plus 1 month"


You can read more about mod_expires on apache's website. http://httpd.apache.org/docs/current/mod/mod_expires.html
darthrevan
I don't know.php off the top of my head. but in php you can do something like if date is DATE do print ''; else print "<img>.....";done
Where DATEs he datevyoy want it to expire.

Though I dunno if this would be what you want.

So it would be like:
Code:
<?php
DATE=`echo(date "F d Y")`
if ($DATE = "November 27 2012");
echo "Picture expired";
else
echo "<img src=......>";

Though I could be wrong a little on the code.
likeabreeze
Peterssidan wrote:
You can use mod_expires.

You can read more about mod_expires on apache's website. http://httpd.apache.org/docs/current/mod/mod_expires.html


From the website I got that it is for Apache Version 2.4,

does it work on Frihost?
darthrevan
Code:
<?php
$date=date("F d Y");
$expire='October 28 2012';
if ("echo($date)"=="$expire" || "$date" > "$expire")
{
echo "Picture expired";
}
else
{
echo "Picture shown";
}

unset($expire, $date);
?>


Works perfectly, tested it. Though of course you'd need to replace the tags for what to do when the picture should expire. So if you dont want to show anything you could do on the first echo , do echo ""; then for the second do echo "<img src=\"IMAGE.jpg\">";

Of course the variable expire would need to be set to the date you want it to expire.
likeabreeze
darthrevan wrote:
Code:
<?php
$date=date("F d Y");
$expire='October 28 2012';
if ("echo($date)"=="$expire" || "$date" > "$expire")
{
echo "Picture expired";
}
else
{
echo "Picture shown";
}
unset($expire, $date);
?>


Thanks for your help.
But how can I set an expiry date for all the images within my website?
Peterssidan
likeabreeze wrote:
From the website I got that it is for Apache Version 2.4,

does it work on Frihost?

I have tested it on my Frihost account and it works. Here is the Apache 2.0 documentation if you want. http://httpd.apache.org/docs/2.0/mod/mod_expires.html
There isn't much of a difference though.

The PHP code posted by darthrevan will not set the expiry date in the HTTP header. Instead it just stops showing the image on the page after a certain date which is not really what was asked for.

The reason Google want you to specify an expiry date is so that the browsers will download the image less often which potentially speeds up the site's loading time and saves bandwidth. Browsers reuse images without reloading them for some time already but it usually doesn't wait as long as a week.
darthrevan
I figured I would post the php code Atleast for another way to hide an image after a date.
Dialogist
Be more specific if you can. It'll really help you out. You can copy and paste this or change it to suit your needs...

Code:

<IfModule mod_expires.c>

# Enable expirations
ExpiresActive On

# Default directive
ExpiresDefault "access plus 1 month"

# My favicon
ExpiresByType image/x-icon "access plus 1 year”

# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"

# CSS
ExpiresByType text/css "access 1 month”

# Javascript
ExpiresByType application/javascript "access plus 1 year"

</IfModule>


Say you tinker with the CSS a lot, like I do, maybe lessen that. But be careful that you don't get too restrictive as the user (you included) needs to clear all cache to see any recent changes made. It's good to define mime types though, as I have gifs that will never change, until perhaps I delete them completely. Favicon is something else that I should be settled on by now, but rarely am. Do what you want with it.

*this is AddDefaultCharset UTF-8 by the way.
Peterssidan
Yeah, you should be careful using ExpiresDefault because it sets the expiry date on all files. Expiry dates is not suitable for dynamic PHP pages and other kind of pages that change often.
Related topics
Laugh out loud
CHEAP DOMAIN FOR YOUR SITE!
Cheap .be Top level Domain Services
Guys, HELP!!! Script wanted!
using sendmail
TCL Products
RANK IMAGES ---- PLEASE LOCK
Win A Domain ..... The Best FriHost Website Edition
PHP Form Question
MySql DATE and ORDERING something by the DATE and displaying
Need MAJOR help with cookies! Mmm.... cookies....
FTP server time
A vision of the ideal economy...
Frihost Reliability?
Reply to topic    Frihost Forum Index -> Webmaster and Internet -> Design Tips

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.