Hi all

I've written a PHP script thats part of a site ive made for an online music library.
The script is parsed a few get parameters and then opens an mp3 from a secure part of the web server that isnt under the public web accessible folders.

All straight forward.

The issue is that in safari, and ONLY safari, i cant make this browser cache the files its download via the php file. every time the url is accessed safari goes and downloads the stream again and never uses its cache.

Ive seen many online examples of code where people are trying to get php to STOP caching, but in my case I want it to. And as I say, ive made this work on the majority of browsers. Its just safari being stubborn.

The basic code i have serving the file is...

$filename = $tracktoplay[0];
$contents = fread(fopen($filename, "rb"), filesize($filename));

header('Content-type: audio/mpeg');
header('Content-length: ' . (string)filesize($filename));

// get date and add a month then format
$str_date = date("D, d M Y H:i:s");// current date
$str_date=strtotime(date("D, d M Y H:i:s", strtotime($str_date)) . " +1 month");
$str_date=gmdate("D, d M Y H:i:s", $str_date)." GMT";

header("Last-Modified: ".$str_date);
header('Expires: '.$str_date);
header('Cache-Control: public'); // HTTP/1.1

echo $contents;

Now ive tried 101 combinations of headers, using many others not in the code shown above such as "Expires", "Pragma" and "ETag". I understand some headers over rule others etc. Anyhow, the mp3 served via this code stays cached in all browsers ive tested except in safari as it just seems to ignore the headers i send or require some other explicit headers to be defined in order to work.

Ive spent ages going through other peoples general experiences of trying to serve files in different ways as well. For example, the main php site has a lot of good examples on their manual page about the header command. Still, nothing has ultimately helped me get around this issue.

Has anyone managed to get safari caching served files??? If so, please let me know the magic combination of headers that made it possible.

Thanks for any help!

    You can have multiple parameters for Cache-control so try setting a max-age parameter that is far in the future and see if that helps

      hi scrupul0us

      thanks for the idea.
      i just changed the cache control line to

      header('Cache-Control: public, max-age=99999, s-maxage=99999');

      and no change im afraid ;(

        hi

        by chance i had already seen this webpage when doing research into the problem. im not 100% sure if thats my issue but ill keep an eye on that. the thing is, Ive tried the latest v4betas and its no better.

          From the code in your first post, you set "last modified" to one month in the future. Perhaps that's messing with safari.

            hi
            ive tried with a few other dates and without the header being sent...
            why do you think this would confuse safari?

            to confirm, i want to make it cache so it should have a date in the future. what would be abnormal about this?

              Because the last time the page was modified obviously can't be in the future. It might have been modified right now, or some time in the past.

              And while I don't know what the spec says about how to handle expires and last modified, it wouldn't be that strange for a browser to disregard expires if it gets a last modified greater than or equal to expires. The only strange part is actually sending a last modified greater than expires.

                hi

                I tried this with a different value for last modified now or in fact just removing it, using alternatives such as "expires". sadly no progress. 🙁

                  detect and redirect safari users to download firefox? :evilgrin:

                    hi scrupul0us...
                    haha if only i could. sadly its not an option.. especially as my client is a mac user and its his main browser!! 🙁 🙁 🙁

                      Write a Reply...