468,315 Members | 1,388 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

Browser caching ?

Hi guys,

Let's say I have a page that is primarily HTML but I added a few small parts
that are PHP, for example a random banner. The page is in no way PHP driven,
it just has some some random stuff like the banner and/or a tip of the day.

What bothers me is that the PHP page won't be cached, so if the page has a
lot of links and, on average, the user clicks ten links (hitting the back
button after he does that) my bandwidth usage increases tenfold.

I think I can send cache headers but I don't know much about them. Is it
possible to set it for, say, an hour so that if he hits the back button
he'll get the same banner/tip of the day that he originally got (because
he's looking at his cached page) and only after an hour will he get a new
random banner/tip ?

I'm ready to switch my pages to PHP but I don't want to get a huge bandwidth
bill.

Any guidence would be appreciated.

Take care,
Cyrus
Jul 17 '05 #1
9 1937
>Let's say I have a page that is primarily HTML but I added a few small parts
that are PHP, for example a random banner. The page is in no way PHP driven,
it just has some some random stuff like the banner and/or a tip of the day.
Can't you turn the URL that returns the *BANNER* into a PHP one,
and leave the main page alone? (The PHP selects a random image,
then returns appropriate headers and the body of the image, probably
with fpassthru()). You get 10 image hits (maybe that's what you
want) but only one hit on the main page. You could also try letting
the browser cache the images with cache headers, but I'm not sure
you want to.
What bothers me is that the PHP page won't be cached, so if the page has a
lot of links and, on average, the user clicks ten links (hitting the back
button after he does that) my bandwidth usage increases tenfold.

I think I can send cache headers but I don't know much about them. Is it
possible to set it for, say, an hour so that if he hits the back button
he'll get the same banner/tip of the day that he originally got (because
he's looking at his cached page) and only after an hour will he get a new
random banner/tip ?

I'm ready to switch my pages to PHP but I don't want to get a huge bandwidth
bill.


Gordon L. Burditt
Jul 17 '05 #2
Cyrus D. wrote:
Hi guys,

Let's say I have a page that is primarily HTML but I added a few small
parts that are PHP, for example a random banner. The page is in no way PHP
driven, it just has some some random stuff like the banner and/or a tip of
the day.

What bothers me is that the PHP page won't be cached, so if the page has a
lot of links and, on average, the user clicks ten links (hitting the back
button after he does that) my bandwidth usage increases tenfold.

I think I can send cache headers but I don't know much about them. Is it
possible to set it for, say, an hour so that if he hits the back button
he'll get the same banner/tip of the day that he originally got (because
he's looking at his cached page) and only after an hour will he get a new
random banner/tip ?

I'm ready to switch my pages to PHP but I don't want to get a huge
bandwidth bill.

Any guidence would be appreciated.


This example at http://butterflies.usenetshit.info/butterfly.phps has the
stuff you need for setting the modification dates and times and telling the
browser to use the cached version where appropriate etc. I've used a
modified version of the code in that page in both PHP and ASP and it worked
nicely.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #3
Hi Gordon,

Let's just say it's text for a random tip of the day. If I make a seperate
PHP file (URL) to return the random tip how do I link it up to my main page
?

Tip.php

<?php
function GetTip(){
//returns a string
}
?>

Main.php

lots of HTML
the tip goes here
more HTML

Sorry if I seem like a newbie.

Thanks,
Cyrus
Jul 17 '05 #4
Cyrus D. schrieb:
Tip.php

<?php
function GetTip(){
//returns a string
}
?>

Main.php

lots of HTML
the tip goes here
more HTML


Main.php

lots of HTML
<?php
include('Tip.php');
$result = GetTip();
echo ($result);
?>
more HTML
Matthias
Jul 17 '05 #5
Cyrus D. wrote:
Let's say I have a page that is primarily HTML but I added a few
small parts that are PHP, for example a random banner.
You already said that, in another thread. And I already answered you, in
that thread. Did you not like my responses?
The page is in no way PHP driven, it just has some some random stuff
like the banner and/or a tip of the day.
I'm not sure what you mean by this. If the page is parsed by php, then
it is php driven. Perhaps you mean that only a small part of the content
is created by php, but that only affects server processing time needed.
It is unrelated to http caching.
What bothers me is that the PHP page won't be cached,
Well, sort of. It means that the php page will not have last-modified
headers, so conditional get requests are impossible. (Didn't I explain
this adequately in the previous thread?)
so if the page has a lot of links and, on average, the user clicks
ten links (hitting the back button after he does that) my bandwidth
usage increases tenfold.
It depends on the browser settings, but this is certainly possible.
I think I can send cache headers but I don't know much about them. Is
it possible to set it for, say, an hour so that if he hits the back
button he'll get the same banner/tip of the day that he originally
got (because he's looking at his cached page) and only after an hour
will he get a new random banner/tip ?
Yes, using php's header function.

header("Cache-Control: max-age=3600");

This sends a response header to the client, as follows,

Cache-Control: max-age=3600

which tells the client that the resource is cacheable, and will be fresh
for 3600 seconds or 1 hour.
I'm ready to switch my pages to PHP
Why? Unless php is doing something critical for the page content, why
not stick with static html? ISTM that you should think about the user.
The user does not need a random banner. So unless that banner is
generating significant revenue, I'd dump the idea. If it is important,
then G. Burditt's idea may be worth exploring. Use a url for the image,
say /image/random.php, and have that url return a random image resource.
That way, the html document that includes that image can remain a static
resource, with all the benefits of http caching (hopefully!) built-in
via Apache.

As for "tip of the day", if that really is what you claim it is, and it
really changes every day, make the page fresh for that day. You may need
to look at the http 1.0 expires header instead. But without details, I
cannot offer you anything more specific.
but I don't want to get a huge bandwidth bill.


If the site is popular, then caching becomes quite critical.

--
Brian (remove "invalid" to email me)
Jul 17 '05 #6
Brian wrote:
What bothers me is that the PHP page won't be cached,


Well, sort of. It means that the php page will not have last-modified
headers, so conditional get requests are impossible. (Didn't I explain
this adequately in the previous thread?)


I'm not sure what you posted in the previous thread so this may cover some
of that as well. You can set last-modified headers with PHP but it's a
little more work than either not doing it, or letting the webserver take
care of it with static HTML files.

I have used this method in a couple of different sites (in both PHP and ASP)
and am going to create a function or class to simplify the process so I can
easily incorporate it into my other projects and will publish it on my
website when it's done.

Anyway, I won't explain it just now but instead point you to the example
code where I found it at http://butterflies.usenetshit.info/butterfly.phps

Hopefully I should have a nice function written by early next week and will
share my version of Brucie's code at that point. (Brucie's the author of
the above script).

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #7
Matthias Esken wrote:
Cyrus D. schrieb:

Tip.php

<?php
function GetTip(){
//returns a string
}
?>

Main.php

lots of HTML
the tip goes here
more HTML

Main.php

lots of HTML
<?php
include('Tip.php');
$result = GetTip();
echo ($result);
?>
more HTML


However, that doesn't solve the caching problem as described. If you're
worried about that side of it, you may want to generate the tip as an
image using GD or something and save it to disk - after all, it's "tip
of the day", not "tip of the request" right? ;) Otherwise, I would say
that the overhead of processing the image for every request may not be
be worthwhile... could just put the tip into an iframe and have that
point to a php script that spits it out I guess..

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #8
Chris Hope wrote:
You can set last-modified headers with PHP but it's a little more
work than either not doing it, or letting the webserver take care of
it with static HTML files.

I have used this method in a couple of different sites (in both PHP
and ASP) and am going to create a function or class to simplify the
process so I can easily incorporate it into my other projects and
will publish it on my website when it's done.


Already done in php:

http://simon.incutio.com/archive/200...conditionalGet

I use a version of that script, modified a bit to do a real time
comparison instead of string matching.

--
Brian (remove "invalid" to email me)
Jul 17 '05 #9
Brian wrote:
Chris Hope wrote:
You can set last-modified headers with PHP but it's a little more
work than either not doing it, or letting the webserver take care of
it with static HTML files.

I have used this method in a couple of different sites (in both PHP
and ASP) and am going to create a function or class to simplify the
process so I can easily incorporate it into my other projects and
will publish it on my website when it's done.


Already done in php:

http://simon.incutio.com/archive/200...conditionalGet

I use a version of that script, modified a bit to do a real time
comparison instead of string matching.


Excellent, thanks. Saves me having to do it myself.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michael G | last post: by
5 posts views Thread by R. Ian Lee | last post: by
21 posts views Thread by nsimeonov | last post: by
3 posts views Thread by jlotmar | last post: by
15 posts views Thread by bvdb | last post: by
3 posts views Thread by Ian Semmel | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.