Browser caching ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cyrus D.

    #1

    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


  • Gordon Burditt

    #2
    Re: Browser caching ?

    >Let's say I have a page that is primarily HTML but I added a few small parts[color=blue]
    >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.[/color]

    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.
    [color=blue]
    >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.[/color]

    Gordon L. Burditt

    Comment

    • Chris Hope

      #3
      Re: Browser caching ?

      Cyrus D. wrote:
      [color=blue]
      > 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.[/color]

      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/

      Comment

      • Cyrus D.

        #4
        Re: Browser caching ?

        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


        Comment

        • Matthias Esken

          #5
          Re: Browser caching ?

          Cyrus D. schrieb:
          [color=blue]
          > Tip.php
          >
          > <?php
          > function GetTip(){
          > //returns a string
          > }
          > ?>
          >
          > Main.php
          >
          > lots of HTML
          > the tip goes here
          > more HTML[/color]

          Main.php

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


          Matthias

          Comment

          • Brian

            #6
            Re: Browser caching ?

            Cyrus D. wrote:
            [color=blue]
            > 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.[/color]

            You already said that, in another thread. And I already answered you, in
            that thread. Did you not like my responses?
            [color=blue]
            > 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.[/color]

            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.
            [color=blue]
            > What bothers me is that the PHP page won't be cached,[/color]

            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?)
            [color=blue]
            > 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.[/color]

            It depends on the browser settings, but this is certainly possible.
            [color=blue]
            > 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 ?[/color]

            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.
            [color=blue]
            > I'm ready to switch my pages to PHP[/color]

            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.
            [color=blue]
            > but I don't want to get a huge bandwidth bill.[/color]

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

            --
            Brian (remove "invalid" to email me)

            Comment

            • Chris Hope

              #7
              Re: Browser caching ?

              Brian wrote:
              [color=blue][color=green]
              >> What bothers me is that the PHP page won't be cached,[/color]
              >
              > 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?)[/color]

              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/

              Comment

              • Justin Koivisto

                #8
                Re: Browser caching ?

                Matthias Esken wrote:
                [color=blue]
                > Cyrus D. schrieb:
                >
                >[color=green]
                >>Tip.php
                >>
                >><?php
                >>function GetTip(){
                >>//returns a string
                >>}
                >>?>
                >>
                >>Main.php
                >>
                >>lots of HTML
                >>the tip goes here
                >>more HTML[/color]
                >
                >
                > Main.php
                >
                > lots of HTML
                > <?php
                > include('Tip.ph p');
                > $result = GetTip();
                > echo ($result);
                > ?>
                > more HTML[/color]

                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 - spam@koivi.com

                Comment

                • Brian

                  #9
                  Re: Browser caching ?

                  Chris Hope wrote:
                  [color=blue]
                  > 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.[/color]

                  Already done in php:



                  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)

                  Comment

                  • Chris Hope

                    #10
                    Re: Browser caching ?

                    Brian wrote:
                    [color=blue]
                    > Chris Hope wrote:
                    >[color=green]
                    >> 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.[/color]
                    >
                    > 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.[/color]

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

                    --
                    Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

                    Comment

                    Working...