Advertisement
dimti

Untitled

Feb 8th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.45 KB | None | 0 0
  1. <?php
  2. /**\
  3.  * @param string $last_modified
  4.  * @param string $etag
  5.  * @return bool
  6.  * @desc original from http://www.nvb.ru/tulz/last~modified~and~etag/
  7.  */
  8. function getIsModified($last_modified = '', $etag = '')
  9. {
  10.     global $_SERVER;
  11.     $refresh = TRUE;
  12.  
  13.     if ($last_modified == '') $last_modified = time();
  14.     $none_match = (isset($_SERVER['HTTP_IF_NONE_MATCH']))
  15.         ? $_SERVER['HTTP_IF_NONE_MATCH'] : '';
  16.     $modified_since = (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
  17.         ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : '';
  18.     $since = 0;
  19.     if ($modified_since) { // BUG: NetScape sends ";lenght = xxx" after the date
  20.         $arraySince = explode(';', $modified_since);
  21.         $since = strtotime($arraySince[0]);
  22.     }
  23.  
  24.     switch (TRUE) {
  25.         case (!$none_match && $modified_since):
  26.             if ($since <= time() && is_int($since) && $since >= $last_modified) return FALSE;
  27.             break;
  28.         case ($none_match):
  29.             if ($modified_since) { // Проверка и по If-None-Match, и по If-Modified-Since
  30.                 if ($since > time() || !is_int($since) || $since < $last_modified)
  31.                     break; // Файл в кеше клиента устарел по If-Modified-Since
  32.             }
  33.             // Проверку If-Modified-Since, если она была - прошли. Проверка по If-None-Match:
  34.             if ($etag == '') break;
  35.             $INM = split('[,][ ]?', $none_match);
  36.             foreach ($INM as $enity)
  37.                 if ($enity == '' || $etag == '' || $enity == '*') return FALSE; // 304 Not Modified
  38.             break;
  39.         default:
  40.             ; // Conditional Get не задан - просто отдаем страницу.
  41.     }
  42.     return TRUE; // Страница изменилась (200 ОК)
  43. }
  44. $is_send_last_modified = false;
  45. function LastModified($last_modified = null) {
  46.     global $is_send_last_modified;
  47.     if (!$last_modified) {
  48.         $last_modified = time();
  49.     }
  50.     $gmt_mtime=gmdate('r', $last_modified);
  51.     if (!$is_send_last_modified) {
  52.         $gmt_mtime = str_replace('+0000', 'GMT', $gmt_mtime);
  53.         header('Last-Modified: ' . $gmt_mtime);
  54.     }
  55.     $is_send_last_modified = true;
  56.     if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  57.         if (!getIsModified($last_modified)) {
  58.             header('HTTP/1.1 304 Not Modified');
  59.             header('Content-Length: 0');
  60.             exit();
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement