티스토리 뷰
파일에 대한 요청이 있을 때, 해당 파일의 마지막 수정 시간을 파일 헤더에 선언한 후 출력한다.
재 요청이 있을 때는 파일 헤더의 마지막 수정 시간과 서버에 해당 파일의 실제 마지막 수정 시간과 비교하여 같다면 HTTP code 304 not modified를 출력하고, 서상의 파일이 더 최근에 수정 된 경우 위의 과정을 반복하여 출력한다.
아래는 예제 소스이다.
//get the last-modified-date of this very file
$lastModified=filemtime(__FILE__);
//get a unique hash of this file (etag)
$etagFile = md5_file(__FILE__);
//get the HTTP_IF_MODIFIED_SINCE header if set
$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
//get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
//set last-modified header
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
//set etag-header
header("Etag: $etagFile");
//make sure caching is turned on
header('Cache-Control: public');
//check if page has changed. If not, send 304 and exit
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])==$lastModified || $etagHeader == $etagFile)
{
header("HTTP/1.1 304 Not Modified");
exit;
}
//your normal code
echo "This page was last modified: ".date("d.m.Y H:i:s",time());
'Programming Language > PHP' 카테고리의 다른 글
| 한글, 한자, 일어 검색 정규식 (0) | 2018.11.07 |
|---|---|
| PHP에서 파일 확장자 구하기 (0) | 2018.11.04 |
| 설치형 방문자 분석 프로그램(Analytics program) (0) | 2018.11.03 |
| CI 3.0에서 하위폴더를 가진 기본 컨트롤러 설정 (0) | 2016.04.21 |
| PHP uksort와 usort 사용자정의 배열 정열 (0) | 2016.04.21 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- express
- Eclipse
- 스프링
- .NET Core
- dotnet core
- high1
- egov
- 하이원
- DART
- CentOS
- php
- Spring
- JSP
- asp.net core
- c#
- eGovFrame
- 이클립스
- Flutter
- Java
- jQuery
- egoverment
- Linux
- Entity Framework Core
- 전자정부
- Windows
- MariaDB
- MySQL
- 자바
- ubuntu
- Fedora
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
글 보관함