公開方法(Public Methods) | |
| httpConditional ($UnixTimeStamp, $cacheSeconds=0, $cachePrivacy=0, $feedMode=false, $compression=false, $session=false) | |
| _httpConditionalCallBack ($buffer, $mode=5) | |
| httpConditionalRefresh ($UnixTimeStamp) | |
定義在 httpcache.class.php 檔案之第 66 行.
|
||||||||||||
|
定義在 httpcache.class.php 檔案之第 175 行. 參考 $mode. 被參考於 httpConditional(). 00175 { 00176 //Private function automatically called at the end of the script when compression is enabled 00177 //rfc2616-sec14.html#sec14.11 00178 //You can adjust the level of compression with zlib.output_compression_level in php.ini 00179 if (extension_loaded('zlib')&&(!ini_get('zlib.output_compression'))) 00180 { 00181 $buffer2=ob_gzhandler($buffer,$mode); //Will check HTTP_ACCEPT_ENCODING and put correct headers 00182 if (strlen($buffer2)>1) //When ob_gzhandler succeeded 00183 $buffer=$buffer2; 00184 } 00185 header('Content-Length: '.strlen($buffer)); //Allows persistant connections //rfc2616-sec14.html#sec14.13 00186 return $buffer; 00187 }
|
|
||||||||||||||||||||||||||||
|
定義在 httpcache.class.php 檔案之第 67 行. 參考 $_SESSION, $_sessionMode, $clientCacheDate, $p, $scriptName, $session, 及 _httpConditionalCallBack(). 被參考於 SmartyView::render(), ResourceServerView::render(), 及 SummaryCachedView::render(). 00069 { 00070 //Credits: http://alexandre.alapetite.net/doc-alex/php-http-304/ 00071 //RFC2616 HTTP/1.1: http://www.w3.org/Protocols/rfc2616/rfc2616.html 00072 //RFC1945 HTTP/1.0: http://www.w3.org/Protocols/rfc1945/rfc1945.txt 00073 00074 if (headers_sent()) return false; 00075 00076 if (isset($_SERVER['SCRIPT_FILENAME'])) $scriptName=$_SERVER['SCRIPT_FILENAME']; 00077 elseif (isset($_SERVER['PATH_TRANSLATED'])) $scriptName=$_SERVER['PATH_TRANSLATED']; 00078 else return false; 00079 00080 if ((!$feedMode)&&(($modifScript=filemtime($scriptName))>$UnixTimeStamp)) 00081 $UnixTimeStamp=$modifScript; 00082 $UnixTimeStamp=min($UnixTimeStamp,time()); 00083 $is304=true; 00084 $is412=false; 00085 $nbCond=0; 00086 00087 //rfc2616-sec3.html#sec3.3.1 00088 $dateLastModif=gmdate('D, d M Y H:i:s \G\M\T',$UnixTimeStamp); 00089 $dateCacheClient='Thu, 10 Jan 1980 20:30:40 GMT'; 00090 00091 //rfc2616-sec14.html#sec14.19 //='"0123456789abcdef0123456789abcdef"' 00092 if (isset($_SERVER['QUERY_STRING'])) $myQuery='?'.$_SERVER['QUERY_STRING']; 00093 else $myQuery=''; 00094 if ($session&&isset($_SESSION)) 00095 { 00096 global $_sessionMode; 00097 $_sessionMode=$session; 00098 $myQuery.=print_r($_SESSION,true).session_name().'='.session_id(); 00099 } 00100 $etagServer='"'.md5($scriptName.$myQuery.'#'.$dateLastModif).'"'; 00101 00102 if ((!$is412)&&isset($_SERVER['HTTP_IF_MATCH'])) 00103 {//rfc2616-sec14.html#sec14.24 00104 $etagsClient=stripslashes($_SERVER['HTTP_IF_MATCH']); 00105 $is412=(($etagClient!='*')&&(strpos($etagsClient,$etagServer)===false)); 00106 } 00107 if ($is304&&isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) 00108 {//rfc2616-sec14.html#sec14.25 //rfc1945.txt 00109 $nbCond++; 00110 $dateCacheClient=$_SERVER['HTTP_IF_MODIFIED_SINCE']; 00111 $p=strpos($dateCacheClient,';'); 00112 if ($p!==false) 00113 $dateCacheClient=substr($dateCacheClient,0,$p); 00114 $is304=($dateCacheClient==$dateLastModif); 00115 } 00116 if ($is304&&isset($_SERVER['HTTP_IF_NONE_MATCH'])) 00117 {//rfc2616-sec14.html#sec14.26 00118 $nbCond++; 00119 $etagClient=stripslashes($_SERVER['HTTP_IF_NONE_MATCH']); 00120 $is304=(($etagClient==$etagServer)||($etagClient=='*')); 00121 } 00122 if ((!$is412)&&isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE'])) 00123 {//rfc2616-sec14.html#sec14.28 00124 $dateCacheClient=$_SERVER['HTTP_IF_UNMODIFIED_SINCE']; 00125 $p=strpos($dateCacheClient,';'); 00126 if ($p!==false) 00127 $dateCacheClient=substr($dateCacheClient,0,$p); 00128 $is412=($dateCacheClient!=$dateLastModif); 00129 } 00130 if ($feedMode) 00131 {//Special RSS/ATOM 00132 global $clientCacheDate; 00133 $clientCacheDate=strtotime($dateCacheClient); 00134 $cachePrivacy=0; 00135 } 00136 00137 if ($is412) 00138 {//rfc2616-sec10.html#sec10.4.13 00139 header('HTTP/1.1 412 Precondition Failed'); 00140 header('Cache-Control: private, max-age=0, must-revalidate'); 00141 header('Content-Type: text/plain'); 00142 echo "HTTP/1.1 Error 412 Precondition Failed: Precondition request failed positive evaluation\n"; 00143 return true; 00144 } 00145 elseif ($is304&&($nbCond>0)) 00146 {//rfc2616-sec10.html#sec10.3.5 00147 header('HTTP/1.0 304 Not Modified'); 00148 header('Etag: '.$etagServer); 00149 if ($feedMode) header('Connection: close'); //Comment this line under IIS 00150 return true; 00151 } 00152 else 00153 {//rfc2616-sec10.html#sec10.2.1 00154 //rfc2616-sec14.html#sec14.3 00155 if ($compression) ob_start('_httpConditionalCallBack'); //Will check HTTP_ACCEPT_ENCODING 00156 //header('HTTP/1.0 200 OK'); 00157 if ($cacheSeconds==0) 00158 { 00159 $cache='private, must-revalidate, '; 00160 //$cacheSeconds=-1500000; //HTTP/1.0 00161 } 00162 elseif ($cachePrivacy==0) $cache='private, '; 00163 elseif ($cachePrivacy==2) $cache='public, '; 00164 else $cache=''; 00165 $cache.='max-age='.floor($cacheSeconds); 00166 // header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T',time()+$cacheSeconds)); //HTTP/1.0 //rfc2616-sec14.html#sec14.21 00167 header('Cache-Control: '.$cache); //rfc2616-sec14.html#sec14.9 00168 header('Last-Modified: '.$dateLastModif); 00169 header('Etag: '.$etagServer); 00170 if ($feedMode) header('Connection: close'); //rfc2616-sec14.html#sec14.10 //Comment this line under IIS 00171 return $_SERVER['REQUEST_METHOD']=='HEAD'; //rfc2616-sec9.html#sec9.4 00172 } 00173 }
|
|
|
定義在 httpcache.class.php 檔案之第 189 行. 參考 $_SESSION, $_sessionMode, 及 $scriptName. 00189 { 00190 //Update HTTP headers if the content has just been modified by the client's request 00191 //See an example on http://alexandre.alapetite.net/doc-alex/compteur/ 00192 if (headers_sent()) return false; 00193 00194 if (isset($_SERVER['SCRIPT_FILENAME'])) $scriptName=$_SERVER['SCRIPT_FILENAME']; 00195 elseif (isset($_SERVER['PATH_TRANSLATED'])) $scriptName=$_SERVER['PATH_TRANSLATED']; 00196 else return false; 00197 00198 $dateLastModif=gmdate('D, d M Y H:i:s \G\M\T',$UnixTimeStamp); 00199 00200 if (isset($_SERVER['QUERY_STRING'])) $myQuery='?'.$_SERVER['QUERY_STRING']; 00201 else $myQuery=''; 00202 global $_sessionMode; 00203 if ($_sessionMode&&isset($_SESSION)) 00204 $myQuery.=print_r($_SESSION,true).session_name().'='.session_id(); 00205 $etagServer='"'.md5($scriptName.$myQuery.'#'.$dateLastModif).'"'; 00206 00207 header('Last-Modified: '.$dateLastModif); 00208 header('Etag: '.$etagServer); 00209 }
|