Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Ochrona plików mp3 przed cache'm Opery
Forum PHP.pl > Forum > PHP
zorkmyst
Witam, zacznę o tego że jestem noobem php wstydnis.gif więc proszę o wyrozumiałość. Pracuję nad stronką, na której użytkownicy mogą odtwarzać pliki mp3. Wykorzystuje w tym celu dewplayer'a z playlistą w xml. Dla ochrony przed bezpośrednim pobieraniem pliki mp3 znajdują się poza public_html serwera. Odwołanie do pliku realizuje za pomocą prostego skryptu php:

  1. <?php
  2.  
  3. $file = "/katalog/poza/public_html/".$_GET["file"].'.mp3';
  4. $fh = fopen($file,"rb");
  5. $mm_type = "application/octet-stream";
  6. while (!feof($fh))
  7. {
  8. header("Cache-Control: no-store, must-revalidate");
  9. header("Pragma: hack");
  10. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  11. header("Content-Type: " . $mm_type);
  12. header('Content-Length: ' . filesize($file));
  13. print(fread($fh, filesize($file)));
  14. }
  15.  
  16. fclose($fh);
  17.  
  18. ?>


plik wywoływany jest na zasadzie skrypt.php?file=nazwapliku. Żeby uprzedzić - do skryptu też nie da się odwołać bezpośrednio. Generalnie wszystko śmiga, problem jest z cache Opery. Za każdym razem kiedy plik jest otwierany w playerze Opera wrzuca go do swojego Cache. Czy istnieje jakiś sposób zabezpieczenia się przed takim zachowaniem Opery? Czy można w jakiś sposób poza headerami ustawić czas wygaśnięcia pliku w cache?

Pozdrawiam
flashdev
Co prawda nie jest to zbyt eleganckie rozwiązanie, ale przynajmniej tymczasowo możesz 'wyłączyć' cache na jeden z poniższych sposobów.

  1. $file = "/katalog/poza/public_html/".$_GET["file"].'.mp3' . '?rnd=' . mt_rand();
  2. header('Location: '.$file);


  1. $file = "/katalog/poza/public_html/".$_GET["file"].'.mp3';
  2. $file .= '?md5=" . md5_file($file);
  3. header('Location: '.$file);
piotr94
a http://php.net/manual/en/function.header.php i Example #2
zorkmyst
Dziękuję za szybką odpowiedź. Nie wiem czy dobrze zaimplementowałem, ale dla rnd:

  1. <?php
  2. $file = "/katalog/poza/public-html/".$_GET["file"].'.mp3' . '?rnd=' . mt_rand();
  3. $fh = fopen($file,"rb");
  4. $mm_type = "application/octet-stream";
  5. while (!feof($fh))
  6. {
  7. header('Location: '.$file);
  8. header("Cache-Control: no-store, no-cache, must-revalidate");
  9. header("Pragma: hack");
  10. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  11. header("Content-Type: " . $mm_type);
  12. header('Content-Length: ' . filesize($file));
  13. print(fread($fh, filesize($file)));
  14. }
  15. fclose($fh);
  16. ?>


serwer generalnie szaleje dostaje w logach:

[error] [client xxx.xxx.xxx.xxx] PHP Warning: fread() expects parameter 1 to be resource, boolean given in skrypt-1.php on line 13
[error] [client xxx.xxx.xxx.xxx] PHP Warning: feof() expects parameter 1 to be resource, boolean given in skrypt-1.php on line 5
[error] [client xxx.xxx.xxx.xxx] PHP Warning: filesize(): stat failed for /katalog/poza/public_html/demo.mp3?rnd=1511147903 in skrypt-1.php on line 12
[error] [client xxx.xxx.xxx.xxx] PHP Warning: filesize(): stat failed for /katalog/poza/public_html/demo.mp3?rnd=1511147903 in skrypt-1.php on line 13
[error] [client xxx.xxx.xxx.xxx] PHP Warning: fread() expects parameter 1 to be resource, boolean given in skrypt-1.php on line 13
[error] [client xxx.xxx.xxx.xxx] PHP Warning: feof() expects parameter 1 to be resource, boolean given in skrypt-1.php on line 5
itd...

dla md5

  1. <?php
  2. $file = "/katalog/poza/public_html/".$_GET["file"].'.mp3';
  3. $file .= '?md5=" . md5_file($file);$fh = fopen($file,"rb");
  4. $mm_type = "application/octet-stream";
  5. while (!feof($fh))
  6. {
  7. header('Location: '.$file);
  8. header("Cache-Control: no-store, no-cache, must-revalidate");
  9. header("Pragma: hack");
  10. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  11. header("Content-Type: " . $mm_type);
  12. header('Content-Length: ' . filesize($file));
  13. print(fread($fh, filesize($file)));
  14. }
  15. fclose($fh);
  16. ?>


w logach:

[error] [client xxx.xxx.xxx.xxx] PHP Parse error: syntax error, unexpected T_STRING in skrypt-2.php on line 7

Testowałem już header'y. Niestety z Operą nic nie działa.

Pozdrawiam
vermis
  1. #
  2. $file .= '?md5=" . md5_file($file);$fh = fopen($file,"rb");

otwierasz apostrofem a zamykasz cudzysłowem.
zorkmyst
Dzięki, mój błąd. Wprowadziłem zmianę i dostaję w logach:

[error] [client xxx.xxx.xxx.xxx] PHP Warning: filesize(): stat failed for /katalog/poza/public_html/.mp3?md5= in skrypt-2.php on line 13
[error] [client xxx.xxx.xxx.xxx] PHP Warning: fread() expects parameter 1 to be resource, boolean given in skrypt-2.php on line 13
[error] [client xxx.xxx.xxx.xxx] PHP Warning: feof() expects parameter 1 to be resource, boolean given in skrypt-2.php on line 5
[error] [client xxx.xxx.xxx.xxx] PHP Warning: filesize(): stat failed for /katalog/poza/public_html/.mp3?md5= in skrypt-2.php on line 12
[error] [client xxx.xxx.xxx.xxx] PHP Warning: filesize(): stat failed for /katalog/poza/public_html/.mp3?md5= in skrypt-2.php on line 13
[error] [client xxx.xxx.xxx.xxx] PHP Warning: fread() expects parameter 1 to be resource, boolean given in skrypt-2.php on line 13
[error] [client xxx.xxx.xxx.xxx] PHP Warning: feof() expects parameter 1 to be resource, boolean given in skrypt-2.php on line 5
[error] [client xxx.xxx.xxx.xxx] PHP Warning: filesize(): stat failed for /katalog/poza/public_html/.mp3?md5= in skrypt-2.php on line 12
[error] [client xxx.xxx.xxx.xxx] PHP Warning: filesize(): stat failed for /katalog/poza/public_html/.mp3?md5= in skrypt-2.php on line 13
[error] [client xxx.xxx.xxx.xxx] PHP Warning: fread() expects parameter 1 to be resource, boolean given in skrypt-2.php on line 13

Skrypt wywołuje na zasadzie skrypt.php?file=demosong

Pozdarawiam
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.