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

php gizp压缩传输js和css文件

May 25, 2016 pm 05:07 PM


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

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

<?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);

            }

        }

    }  

Copy after login

                   

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)