<?php
##
## this file name is 'func.url.php'
##
## url functions
##
## [author]
##  - Chilbong Kim<san2(at)linuxchannel.net>
##
## [changes]
##  - 2003.09.24 : bug fixed
##  - 2003.08.12 : add header_filecache()
##  - 2003.06.10 : add header_nocache(), header_cache()
##  - 2003.03.13 : new build
##
## [references]
##
## [usage]
##
## [example]
##

function url_refresh($url$sec=0)
{
  echo 
"<META http-equiv='Refresh' content='$sec; URL=$url'>";
  exit;
}

function 
url_header($url)
{
  
Header('Location: '.$url);
  exit;
}

## HTML page no cache
##
## use the 'action' PHP file header
## use GMT timezone instead of system default timezone(ex KST,...)
##
## echo '<META HTTP-EQUIV="Expires" CONTENT="Fri, Jun 12 1981 08:20:00 GMT">'."\n";
## echo '<META HTTP-EQUIV="Pragma" CONTENT="no-cache">'."\n";
## echo '<META HTTP-EQUIV="Cache-Control" CONTENT="no cache, must revalidate">."\n";
## or
## session_cache_limiter('nocache');
## session_start();
##
function header_nocache()
{
  
header('Expires: Thu, 19 Nov 1981 08:52:00 GMT'); // alway past time
  
header('Cache-Control: no-store, no-cache, must-revalidate, '.
    
'post-check=0, pre-check=0');
  
header('Pragma: no-cache'); // HTTP/1.0
  //header('Last-Modified:  '.gmdate('D, d M Y H:i:s T')); // not need

  
return TRUE// alway true
}

## HTML page cache
##
## use the 'write HTML FORM' file header
## use GMT timezone instead of system default timezone(ex KST,...)
##
## [same as]
##  session_cache_limiter('private');
##  session_start();
##
## [study]
##  - if 'Expires' is futher time, then don't alway access(request) to server.
##  - if 'Expires' is past time, it is alway access to server
##
## [refrences]
##  - RFC 2616(HTTP/1.1) http://www.w3.org/Protocols/rfc2616/rfc2616.html
##  - http://kr.php.net/manual/en/function.session-cache-limiter.php
##
## [argument]
##  - $expire    integer, expire seconds, default 86400 seconds(1 days), inputing time
##
## [return]
##  - TRUE
##
function header_cache($expire=86400)
{
  if(!
$expire || $expire <= 0$expire 86400// refer to 'session.cache_expire'

  
header('Expires: '.gmdate('D, d M Y H:i:s T',time()-$expire)); // past or future time
  
header('Last-Modified: '.gmdate('D, d M Y H:i:s T'));
  
header('Cache-control: private, max-age='.$expire.', pre-check='.$expire);

  return 
TRUE// alway true
}

## static or vitural image file caching
##
function header_filecache($days=1$file='')
{
  global 
$_SERVER// for PHP/4.0.x

  
if($file && !@file_exists($file)) return FALSE;

  
$term = (int)$days// alway $days > 0
  
$req $_SERVER['HTTP_IF_MODIFIED_SINCE']; // request modified datetime

  
$time = array();
  
$time['expire'] = $term 86400// expire term, after cached
  
$time['client'] = $term 300// client 'think time', or keepalive

  
if($file)
  {
    
$time['cur'] = $time['chk'] = @filemtime($file);
  } else
  {
    
$time['cur'] = time(); // virual file mtime
    
$time['chk'] = $time['cur'] - $time['expire'];
  }

  if(
$req && strtotime($req)>=$time['chk'])
  {
    
header('HTTP/1.1 304 Not Modified');
    exit; 
// don't print any messages
  
}

  
header('Expires: '.gmdate('D, d M Y H:i:s T',$time['cur']+$time['expire']));
  
header('Last-Modified: '.gmdate('D, d M Y H:i:s T',$time['cur'])); // GMT
  
header('Cache-control: private, max-age='.$time['client'].', pre-check='.$time['client']);

  return 
TRUE// alway true
}

?>