찾다
php教程PHP源码php gizp压缩传输js和css文件


<?php

    /**
	 *  完整调用示例:
	 *  1、combine.php?t=j&b=public&fs=jslib.jquery,function
	 *  
	 *  该例子调用的是网站根目录下的public/jslib/jquery.js和public/function.js

	 *  
	 *  2、combine.php?t=j&fs=jslib.jquery,function
	 *  
	 *  该例子调用的是网站根目录下的jslib/jquery.js和function.js
	 *  
	 *  3、combine.php?t=c&b=public.css&fs=common,index
	 *  
	 *  该例子调用的是网站根目录下的public/css/common.css和public/css/index.css
	 *  
	 *  4、combine.php?t=c&fs=css.common
	 *  该例子调用的是网站根目录下的css/common.css
	 *  
	 *  注:多个文件名之间用,分隔;只有一个文件名最后不要有,
	 *     用,分隔的多个文件会被压缩进一个文件,一次性传给浏览器
	 **/
	$is_bad_request=false;
	$cache = true;
	$doc_root_uri=$_SERVER[&#39;DOCUMENT_ROOT&#39;].&#39;/&#39;;
	$cachedir = $doc_root_uri . &#39;public/cache&#39;;
	//文件类型,j为js,c为css
	$type=isset($_GET[&#39;t&#39;])?($_GET[&#39;t&#39;]==&#39;j&#39;||$_GET[&#39;t&#39;]==&#39;c&#39;?$_GET[&#39;t&#39;]:&#39;&#39;):&#39;&#39;;
	
	//存放js和css文件的基目录, 例如:?b=public.js 代表的是/public/js文件夹,出发点是网站根目录
	//基目录参数不是必须的,如果有基目录那么这个基目录就会附加在文件名之前
    $base =isset($_GET[&#39;b&#39;])?($doc_root_uri.str_replace(&#39;.&#39;,&#39;/&#39;,$_GET[&#39;b&#39;])):$doc_root_uri;
	
	//文件名列表,文件名不带后缀名.比如基目录是
	//文件名的格式是 :基目录(如果有)+文件包名+文件名
	//例如:类型是j,
	//     文件名public.js.jquery
	//     如果有基路径且为public,
	//     那么转换后的文件名就是/public/public/js/jquery.js
	//     如果没有基路径
	//     那么转换后的文件名就是/public/js/jquery.js
	//多个文件名之间用,分隔
	$fs=isset($_GET[&#39;fs&#39;])?str_replace(&#39;.&#39;,&#39;/&#39;,$_GET[&#39;fs&#39;]):&#39;&#39;;
	$fs=str_replace(&#39;,&#39;,&#39;.&#39;.($type==&#39;j&#39;?&#39;js,&#39;:&#39;css,&#39;),$fs);
	$fs=$fs.($type==&#39;j&#39;?&#39;.js&#39;:&#39;.css&#39;);
	
	
	
	if($type==&#39;&#39;||$fs==&#39;&#39;){$is_bad_request=true;}
	//die($base);
	///////////////http://blog.ddian.cn
	if($is_bad_request){header ("HTTP/1.0 503 Not Implemented");}
	
	
	$file_type=$type==&#39;j&#39;?&#39;javascript&#39;:&#39;css&#39;;
	
	$elements = explode(&#39;,&#39;,preg_replace(&#39;/([^?]*).*/&#39;, &#39;\1&#39;, $fs));
	
	// Determine last modification date of the files
	$lastmodified = 0;
	while (list(,$element) = each($elements)) {
		$path =$base . &#39;/&#39; . $element;
	
		if (($type == &#39;j&#39; && substr($path, -3) != &#39;.js&#39;) || 
			($type == &#39;c&#39; && substr($path, -4) != &#39;.css&#39;)) {
			header ("HTTP/1.0 403 Forbidden");
			exit;	
		}
	
		if (substr($path, 0, strlen($base)) != $base || !file_exists($path)) {
			header ("HTTP/1.0 404 Not Found");
			exit;
		}
		
		$lastmodified = max($lastmodified, filemtime($path));
	}
	
	// Send Etag hash
	$hash = $lastmodified . &#39;-&#39; . md5($fs);
	header ("Etag: \"" . $hash . "\"");
	
	if (isset($_SERVER[&#39;HTTP_IF_NONE_MATCH&#39;]) && 
		stripslashes($_SERVER[&#39;HTTP_IF_NONE_MATCH&#39;]) == &#39;"&#39; . $hash . &#39;"&#39;) 
	{
		// Return visit and no modifications, so do not send anything
		header ("HTTP/1.0 304 Not Modified");
		header ("Content-Type: text/" . $file_type);
		header (&#39;Content-Length: 0&#39;);
	} 
	else 
	{
		// First time visit or files were modified
		if ($cache) 
		{
			// Determine supported compression method
			$gzip = strstr($_SERVER[&#39;HTTP_ACCEPT_ENCODING&#39;], &#39;gzip&#39;);
			$deflate = strstr($_SERVER[&#39;HTTP_ACCEPT_ENCODING&#39;], &#39;deflate&#39;);
	
			// Determine used compression method
			$encoding = $gzip ? &#39;gzip&#39; : ($deflate ? &#39;deflate&#39; : &#39;none&#39;);
	
			// Check for buggy versions of Internet Explorer
			if (!strstr($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;Opera&#39;) && 
				preg_match(&#39;/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i&#39;, $_SERVER[&#39;HTTP_USER_AGENT&#39;], $matches)) {
				$version = floatval($matches[1]);
				
				if ($version < 6)
					$encoding = &#39;none&#39;;
					
				if ($version == 6 && !strstr($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;EV1&#39;)) 
					$encoding = &#39;none&#39;;
			}
			
			// Try the cache first to see if the combined files were already generated
			$cachefile = &#39;cache-&#39; . $hash . &#39;.&#39; . $file_type . ($encoding != &#39;none&#39; ? &#39;.&#39; . $encoding : &#39;&#39;);
			
			if (file_exists($cachedir . &#39;/&#39; . $cachefile)) {
				if ($fp = fopen($cachedir . &#39;/&#39; . $cachefile, &#39;rb&#39;)) {

					if ($encoding != &#39;none&#39;) {
						header ("Content-Encoding: " . $encoding);
					}
				
					header ("Content-Type: text/" . $file_type);
					header ("Content-Length: " . filesize($cachedir . &#39;/&#39; . $cachefile));
					fpassthru($fp);
					fclose($fp);
					exit;
				}
			}
		}
	
		// Get contents of the files
		$contents = &#39;&#39;;
		reset($elements);
		while (list(,$element) = each($elements)) {
			$path = $base . &#39;/&#39; . $element;
			$contents .= "\n\n" . file_get_contents($path);
		}
	
		// Send Content-Type
		header ("Content-Type: text/" . $file_type);
		
		if (isset($encoding) && $encoding != &#39;none&#39;) 
		{
			// Send compressed contents
			$contents = gzencode($contents, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
			header ("Content-Encoding: " . $encoding);
			header (&#39;Content-Length: &#39; . strlen($contents));
			echo $contents;
		} 
		else 
		{
			// Send regular contents
			header (&#39;Content-Length: &#39; . strlen($contents));
			echo $contents;
		}

		// Store cache
		if ($cache) {
			if ($fp = fopen($cachedir . &#39;/&#39; . $cachefile, &#39;wb&#39;)) {
				fwrite($fp, $contents);
				fclose($fp);
			}
		}
	}	

                   

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.